Reputation: 165
Hi is the following two are equivalent:
template<class T>
class name {
public:
name() {/*...*/}
name(name const &o) {/*...*/} // WITHOUT TEMPLATE ARGUMENT
/*...*/
};
template<class T>
class name {
public:
name() {/*...*/}
name(name<T> const &o) {/*...*/} // WITH TEMPLATE ARGUMENT SPECIFIED
/*...*/
};
So my question is this: Do I have to write classname with or without the template argument list in the copy constructor or not? If I don't write the template arguments does it mean that then other versions (with different template argument) can be passed to the copy constructor?
So if I want to achive that from the class A with template argument e.g.: int, the copy constructor of it only accept A with the same template argument (in our example: int), do I have to put the template arguments (< T, K, ...>) there?
Upvotes: 3
Views: 304
Reputation: 7687
inside the template definition, name
is a shorthand for name<T>
( see §14.6.1 )
Upvotes: 2
Reputation: 254601
Do I have to write classname with or without the template argument list in the copy constructor or not?
They are equivalent. Within the template's scope, name
is the injected class name, denoting the class name<T>
. It can also be used as the template name, if you specify template arguments.
If I don't write the template arguments does it mean that then other versions (with different template argument) can be passed to the copy constructor?
No, without arguments it specifically denotes name<T>
. To allow conversion from other specialisations, you'd need a constructor template:
template <typename T2> name(name<T2> const & other);
Note that this won't act as a copy constructor: you'll need to declare that separately if you don't want the implicitly generated one.
So if I want to achive that [...] it only accept A with the same template argument [...], do I have to put the template arguments (< T, K, ...>) there?
No, what you have is fine in that case.
Upvotes: 3