Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda is deduced to std::function if template has no variadic arguments

template<typename ReturnT, typename... ParamT>
void foo(std::function<ReturnT(ParamT...)> callback)
{}

template<typename ReturnT, typename ParamT>
void bar(std::function<ReturnT(ParamT)> callback)
{}

main()
{    
    foo<int, int>([](int x){ return x; });  // no instance of function 
                                            //   template matches argument list
    bar<int, int>([](int x){ return x; });  // OK
}

The only difference between foo and bar is that foo has variadic arguments. Somehow the compiler is able to convert the lambda to a std::function in bar.

To my understanding, template type deduction doesn't consider type conversions. So shouldn't both fail?

like image 420
pong Avatar asked Oct 17 '18 08:10

pong


1 Answers

You don't have any deduction for the type parameters of bar, they are fully specified.

You still have the tail of the pack to deduce in foo, and that fails because the lambda isn't a std::function.

like image 145
Caleth Avatar answered Nov 15 '22 18:11

Caleth