Default lambda as templated parameter of a function

后端 未结 5 482
悲&欢浪女
悲&欢浪女 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:12

    You are trying to say something that makes no sense. You are asking the compiler to guess T from your arguments, but then you do not provide any argument.

    The following code does compile, and does what you want:

    template void foo(const T& t) {
      // implementation here
    }
    
    template void foo() {
      foo([]() {}); // Call actual implementation with empty lambda
    }
    
    void bar() {
      foo([&](){ /* implementation here */ }); // this compiles
      foo(); // this now compiles as well
    }
    

提交回复
热议问题