How should I define a std::function variable with default arguments?

后端 未结 4 1068
有刺的猬
有刺的猬 2021-02-07 08:48

To set a std::function variable to a lambda function with default argument I can use auto as in:

auto foo = [](int x = 10){cout << x <<          


        
4条回答
  •  走了就别回头了
    2021-02-07 09:32

    Don't know if that will help you, but you can store a lambda in a templated struct.

    template 
    struct Bar {
      F foo;
      Bar (F fun): foo (std::move (fun)) {}
    };
    
    auto f = [](int x = 10) {cout << x << endl;};
    Bar bar (f);
    bar.foo();
    
    auto makeFun = [](){return [](int x = 10) {cout << x << endl;};};
    
    Bar bar2 (makeFun());
    bar2.foo();
    

提交回复
热议问题