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

后端 未结 4 1071
有刺的猬
有刺的猬 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:37

    One way you could solve this would be to wrap your std::function in a functor object which implements the default arguments for you:

    struct MyFunc
    {
        void operator()(int x = 10) { f(x); }
        std::function f;
    };
    
    struct Bar
    {
        MyFunc foo = {[](int x){std::cout << x << "\n";}};
    };
    
    int main() {
        Bar bar;
        bar.foo();
    }
    

提交回复
热议问题