Can you tell me how to invoke template constructor explicitly (in initializer list)?
for example:
struct T {
template<class> T();
};
struct U {
U() : t<void>() {} //does not work
T t;
};
thanks
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 not possible. The Standard also has a note on this at
14.8.1/7Explanation: This says: Template arguments are passed in angle brackets after a function template name, such as
std::make_pair<int, bool>. And constructors don’t have a name of their own, but they abuse their class names in various contexts (soU<int>()means: Pass<int>to the class templateU, and construct an object by calling the default constructor without arguments). Therefore, one cannot pass template arguments to constructors.In your case, you are trying to pass template arguments in a member initializer. In that case, there’s even more of a problem: It will attempt to parse and interpret
t<void>as a base-class type and thinks you want to call the default constructor of a base class. This will fail, of course.If you can live with it, you can work it around
Given
identitylike it’s defined in boostWithin C++20 you can use
std::type_identityas identity type.