Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a templated constructor like 'template<class T> X(){}'?

struct X{
    template<class T>
    X(){}
};

Is it possible to instantate such a type?

like image 772
Xeo Avatar asked Feb 02 '23 17:02

Xeo


1 Answers

Yes, it is possible to have such a constructor, but it's impossible to call it. All the template parameters of a templated constructor must be deduced from the parameter list or have a default value. In Your example you can't instantiate the class.

[temp.mem]

[ Note: Because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. —end note ]

like image 113
Yakov Galka Avatar answered May 13 '23 00:05

Yakov Galka