Is it possible to construct a std::function with the constructor of a type defined by a template argument?
For example:
template <typename T>
bool registerType()
{
const std::function<T()> func = &T::T; //I know this doesn't work
//...
}
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.
I don’t think so, because constructors don’t have names, you can’t take a pointer/reference to them, and in general they don’t behave quite like functions.
You could use a lambda to initialize a
std::functionwith the same signature:Calling it produces the same result as using the expression
T()to construct a temporary of typeT, but possibly with different side-effects. In the case of a true function call there’s an extra temporary in the return statement, which nominally is copied/moved to the return value. The implementation may or may not elide the extra temporary.