I want get the type of lambda as the template argument. How to do this?
template<class T>
class Foo
{
public:
Foo(T t){ t(); }
};
int main()
{
Foo< type?? > foo([](){ cout<<"construct OK"; });
system("pause");
return 0;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s possible to deduce the type of a lambda-expression when it is the argument to a function template function parameter whose type is deduced.
Lambda expressions however are explicitly forbidden to appear inside an unevaluated operand — this includes
decltype. It was found that this includes several yet unhandled special cases and it was found that there is no real utility to allow lambda expressions insidedecltypeand friends. Recall that every lambda expression creates a new unique type.You can instead use
std::function<void()>, which is able to store any function object that can be called without arguments and yieldsvoidas return type.Of course, as I said above, function templates can deduce their argument type, so you can also make the constructor a template
But I take it that this is not really useful in your case, since you presumably want to do something with
Tinside your class definition. Like, storing the object as a non-static data member. Withstd::function, that’s possible.