Default lambda as templated parameter of a function

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

    The compiler uses the arguments passed to deduce the template type. If there's no arguments, then how would the compiler be able to deduce the template type?

    You can use overloading instead of default arguments here.

    The overloaded non-argument function can simply call the function with the "default" argument:

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

提交回复
热议问题