简体   繁体   English

模板类复制构造函数参数是否带有模板参数?

[英]Template class copy constructor parameter with or without template argument?

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 eg: 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? 所以,如果我想才达到从A类与模板参数如:INT,它的拷贝构造函数只接受相同的模板参数(在我们的例子:int),我必须把模板参数(<T ,K,...>)在那儿吗?

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> . 在模板范围内, name注入的类名 ,表示类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> . 不,没有参数,它专门表示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? 因此,如果我想获得仅接受具有相同模板参数[...]的A,我是否必须在此处放置模板参数(<T,K,...>)?

No, what you have is fine in that case. 不,在这种情况下,您所拥有的没问题。

在模板定义中, namename<T>的简写(请参见第14.6.1节)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM