复制模板类的构造函数 [英] Copy constructor of template class

查看:248
本文介绍了复制模板类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到模板copy-con从不是默认的复制onstructor,模板赋值操作从不是一个复制赋值操作符。



我不明白为什么需要这个限制,并且立即上网到ideone,并返回测试程序,但这里的复制构造函数永远不会调用进一步googling我遇到模板化的构造函数,并尝试,但仍然从来没有调用复制构造函数。

  #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>一个; return a;
}

int main(){
//你的代码在这里
tt< int>一个 ; a = test();

return 0;
}

有人可以解释我这个限制背后的全部原因,一个模板类的复制构造函数。



感谢

解决方案

严格规则什么构成复制构造函数(cf. C ++ 11,12.8):




  • 它不是模板。 / p>


  • 对于类 T ,其第一个参数必须具有 T const& T volatile&


  • 如果它有多个参数,进一步的参数必须有默认值。




如果不声明复制构造函数,则 T :: T T const&)隐式声明。 (它可能或可能不会被实际定义,如果它被定义,它可能被定义为已删除。)



(通常的重载解析规则意味着你可以)



对于move构造函数有类似的规则,&& 代替&


I read that template copy-con is never default copy onstructor, and template assignment-op is never a copy assignment operator.

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.

#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):

  • 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 &.

  • 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. (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 &.

这篇关于复制模板类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆