模板 class 使用或不使用模板参数复制构造函数参数?
Template class copy constructor parameter with or without template argument?
嗨是下面两个是等价的:
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
/*...*/
};
所以我的问题是:我必须写 classname with 还是 without 复制构造函数中的模板参数列表是否存在?如果我不写模板参数是否意味着其他版本(具有不同的模板参数)可以传递给复制构造函数?
因此,如果我想从 class A 中使用模板参数实现,例如:int,复制构造函数它只接受 A 和 same 模板参数(在我们的示例中:int),我必须将模板参数 (< T, K, ...>) 放在那里?
Do I have to write classname with or without the template argument list in the copy constructor or not?
它们是等价的。在模板范围内,name
是 注入的 class 名称 ,表示 class name<T>
。如果您指定模板参数,它也可以用作模板名称。
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?
不,没有参数它具体表示name<T>
。要允许从其他专业转换,您需要一个构造函数模板:
template <typename T2> name(name<T2> const & other);
请注意,这不会充当复制构造函数:如果您不想要隐式生成的构造函数,则需要单独声明它。
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?
不,那样的话你有什么就可以了。
在模板定义中,name
是 name<T>
的 shorthand
(参见第 14.6.1 节)
嗨是下面两个是等价的:
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
/*...*/
};
所以我的问题是:我必须写 classname with 还是 without 复制构造函数中的模板参数列表是否存在?如果我不写模板参数是否意味着其他版本(具有不同的模板参数)可以传递给复制构造函数?
因此,如果我想从 class A 中使用模板参数实现,例如:int,复制构造函数它只接受 A 和 same 模板参数(在我们的示例中:int),我必须将模板参数 (< T, K, ...>) 放在那里?
Do I have to write classname with or without the template argument list in the copy constructor or not?
它们是等价的。在模板范围内,name
是 注入的 class 名称 ,表示 class name<T>
。如果您指定模板参数,它也可以用作模板名称。
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?
不,没有参数它具体表示name<T>
。要允许从其他专业转换,您需要一个构造函数模板:
template <typename T2> name(name<T2> const & other);
请注意,这不会充当复制构造函数:如果您不想要隐式生成的构造函数,则需要单独声明它。
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?
不,那样的话你有什么就可以了。
在模板定义中,name
是 name<T>
的 shorthand
(参见第 14.6.1 节)