简体   繁体   English

复制模板类的构造函数

[英]Copy constructor of template class

I read that template copy-con is never default copy onstructor, and template assignment-op is never a copy assignment operator. 我读到模板copy-con永远不是默认的onstructor,而模板赋值操作永远不是复制赋值操作符。

I couldn't understand why this restriction is needed and straight away went online to ideone and return a test program but 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 谢谢

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 type T & or T const & or T volatile & or T const volatile & . 对于类T ,其第一个参数必须具有类型T &T const &T volatile &T const volatile &

  • If it has more than one argument, the further arguments must have default values. 如果它有多个参数,则其他参数必须具有默认值。

If you do not declare a copy constructor, a copy constructor of the form T::T(T const &) is implicitly declared for you. 如果未声明复制构造函数,则会隐式声明 T::T(T const &)形式的复制构造T::T(T const &) (It may or may not actually be defined, and if it is defined it may be defined as deleted.) (它可能实际上也可能没有定义,如果定义它可能被定义为已删除。)

(The usual overload resolution rules imply that you can have at most four copy constructors, one for each CV-qualification.) (通常的重载决策规则意味着最多可以有四个复制构造函数,每个复制构造函数一个。)

There are analogous rules for move constructors, with && in place of & . 移动构造函数有类似的规则,用&&代替&

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>

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

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