c++ - automatic decay of lambda to function pointer when passing to template function -
is there way make lambda decay pointer, without explicitly casting right signature? tidy code:
template<typename t> t call(t(*func)()){ return func(); } int ptr(){ return 0; } int main(){ auto ret1 = call(ptr); auto ret2 = call((int(*)())([]{ return 0; })); auto ret3 = call([]{ return 0; }); //won't compile }
it's evident call call
works if lambda decays pointer, i'm guessing that can happen after right function overload/template chosen. unfortunately can think of solutions involve templates make lambda with signature decay, i'm square one.
you can change lambda use unary +
operator: +[]{ return 0; }
this works because unary plus can applied pointers, , trigger implicit conversion function pointer.
Comments
Post a Comment