C++ 模板类的复制构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19167201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy constructor of template class
提问by anonymous
I read that template copy-con is never default copy onstructor, and template assignment-op is never a copy assignment operator.
我读到模板 copy-con 从来都不是默认的 copy onstructor,模板 assignment-op 从来都不是复制赋值运算符。
I couldn't understand why this restriction is needed and straight away went online to ideone and return a test programbut here copy constructor never gets called on further googling I came across templatized constructor and tried that but still it never calls copy constructor.
我不明白为什么需要这个限制,并立即上线到 ideone 并返回一个测试程序,但在这里复制构造函数从未被进一步谷歌搜索调用我遇到了模板化的构造函数并尝试过,但它仍然从未调用复制构造函数。
#include <iostream>
using namespace std;
template <typename T> class tt
{
public :
tt()
{
std::cout << std::endl << " CONSTRUCTOR" << std::endl;
}
template <typename U> const tt<T>& operator=(const tt<U>& that){std::cout << std::endl << " OPERATOR" << std::endl;}
template <typename U> tt(const tt<U>& that)
{
std::cout << std::endl << " COPY CONSTRUCTOR" << std::endl;
}
};
tt<int> test(void)
{
std::cout << std::endl << " INSIDE " << std::endl; tt<int> a; return a;
}
int main() {
// your code goes here
tt<int> a ; a = test();
return 0;
}
Can someone explain me the whole reason behind putting this restriction and also how to write a copy constructor of template class.
有人可以向我解释施加此限制的全部原因以及如何编写模板类的复制构造函数。
Thanks
谢谢
回答by Kerrek SB
There are strict rules what constitutes a copy constructor (cf. C++11, 12.8):
复制构造函数的构成有严格的规则(参见 C++11, 12.8):
It is not a template.
For a class
T
, its first argument must have typeT &
orT const &
orT volatile &
orT const volatile &
.If it has more than one argument, the further arguments must have default values.
它不是模板。
对于一个类
T
,它的第一个参数必须是T &
orT const &
或T volatile &
or类型T const volatile &
。如果它有多个参数,则其他参数必须具有默认值。
If you do not declare a copy constructor, a copy constructor of the form T::T(T const &)
is implicitly declaredfor you. (It may or may not actually be defined, and if it is defined it may be defined as deleted.)
如果您不声明复制构造函数,T::T(T const &)
则会为您隐式声明该形式的复制构造函数。(它实际上可能会也可能不会被定义,如果它被定义,它可能被定义为删除。)
(The usual overload resolution rules imply that you can have at most four copy constructors, one for each CV-qualification.)
(通常的重载解析规则意味着您最多可以有四个复制构造函数,每个 CV 限定一个。)
There are analogous rules for move constructors, with &&
in place of &
.
移动构造函数也有类似的规则,用&&
代替&
。
回答by SirGuy
I can't comment on why this is how it is, but here's how you write a copy constructor and assignment operator for a class template:
我无法评论为什么会这样,但这是为类模板编写复制构造函数和赋值运算符的方法:
template <class T>
class A
{
public:
A(const A &){}
A & operator=(const A& a){return *this;}
};
and that's it.
The trick here is that even though A
is a template, when you refer to it inside the class as A
(such as in the function signatures) it is treated as the full type A<T>
.
就是这样。
这里的技巧是,即使A
是模板,当您在类中引用它时A
(例如在函数签名中),它也会被视为完整类型A<T>
。