Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Can a template constructor be a non templated constructor?


Asked by Gatlin Jimenez on Dec 01, 2021 FAQ



If you provide a template constructor for class X which takes T const & or T or T& then the compiler will nevertheless generate a default non-templated copy-constructor, even though you may think that it shouldn't because when T = X the declaration matches the copy-constructor declaration.
Furthermore,
If you declare any constructor (including a templated one), the compiler will refrain from declaring a default constructor. Unless you declare a copy-constructor (for class X one that takes X or X& or X const &) the compiler will generate the default copy-constructor.
Next, Therefore, you cannot explicitly specify template arguments for constructor. You can't explicitly specify the template arguments for a constructor, because the constructor has no name on its own, and so there's no syntax for it. use a factory function.
In addition,
You cannot use T for the class template parameter and the constructor template parameter. But, to answer your question, from [14.5.2p5]:
Accordingly,
Using the UseType<T> () function when calling the constructor makes it clear to someone looking at the code that the purpose of that parameter is to tell the constructor what type to use. One use case for this would be if the constructor creates a derived class object and assigns it to a member variable that is a base class pointer.