Default lambda as templated parameter of a function

后端 未结 5 474
悲&欢浪女
悲&欢浪女 2021-01-22 02:07

Consider the following code

template void foo(const T& t = []() {}) {
  // implementation here
}

void bar() {
  foo([&         


        
5条回答
  •  野的像风
    2021-01-22 02:18

    Consider overloading it directly:

    template 
    void foo(void) {
      foo([](){});
    }
    

    See CppReference:

    Non-deduced contexts

    4) A template parameter used in the parameter type of a function parameter that has a default argument that is being used in the call for which argument deduction is being done:

    Type template parameter cannot be deduced from the type of a function default argument: template void f(T = 5, T = 7);

    void g()
    {
        f(1);     // OK: calls f(1, 7)
        f();      // error: cannot deduce T
        f(); // OK: calls f(5, 7)
    }
    

提交回复
热议问题