模板类上的C ++模板副本构造函数 [英] C++ template copy constructor on template class

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

问题描述

我有一个带有模板副本构造函数的模板类。问题是当我使用具有相同模板类型的该类的另一个实例实例化该类时,未调用我的模板副本构造函数。为什么不匹配?

I have a template class that has a template copy constructor. The problem is when I instantiate this class using another instance of this class with the same template type, my template copy constructor is not called. Why doesn't it match?

这是代码段:

#include <iostream>

template <typename T>
class MyTemplateClass
{
    public:
        MyTemplateClass()
        {
            std::cout << "default constructor" << std::endl;
        }

        /*
        MyTemplateClass(const MyTemplateClass<T>& other)
        {
            std::cout << "copy constructor" << std::endl;
        }
        */

        template <typename U>
        MyTemplateClass(const MyTemplateClass<U>& other)
        {
            std::cout << "template copy constructor" << std::endl;
        }
};

int main()
{
    MyTemplateClass<int> instance;
    MyTemplateClass<int> instance2(instance);
    return EXIT_SUCCESS;
}

输出为

default constructor

但是如果我明确编写默认副本构造函数(通过取消注释),输出将变为

But if I explicitly write the default copy constructor (by uncommenting it), then the output becomes

default constructor
copy constructor

我真的不明白。我使用本地编译器(Clang 500.2.79)和此进行了测试。 a>(GCC 4.9.2)并得到相同的结果。

I really don't get it. I tested it with my local compiler (Clang 500.2.79) and with this one (GCC 4.9.2) and got the same result.

推荐答案

副本构造函数的形式为 X(X&)(X const&),如果您自己没有声明,编译器会为您提供(或此处不相关的其他一些条件)。您没有,所以我们暗含了以下一组候选人:

A copy constructor is of the form X(X& ) or (X const&) and will be provided for you by the compiler if you didn't declare one yourself (or a few other conditions which are not relevant here). You didn't, so implicitly we have the following set of candidates:

MyTemplateClass(const MyTemplateClass&);
template <typename U> MyTemplateClass(const MyTemplateClass<U>&);

两者都可行

MyTemplateClass<int> instance2(instance);

两者都采用相同的参数。问题不在于您的副本构造函数模板 match 。问题是隐式副本构造函数不是函数模板,在涉及重载解析时,非模板优先于模板专业化。从[over.match.best]中,省略无关的要点:

Both take the same exact arguments. The issue isn't that your copy constructor template doesn't match. The issue is that the implicit copy constructor is not a function template, and non-templates are preferred to template specializations when it comes to overload resolution. From [over.match.best], omitting the unrelated bullet points:


鉴于这些定义,一个可行的函数F1被定义为如果对于所有参数i,ICS i (F1)的转换顺序不比ICS i (F2)差,则该函数比另一个可行函数
F2更好。然后

— [...]

— F1不是函数模板特化,而F2不是函数模板特化,或者如果不是,则

— [...]

Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then
— [...]
— F1 is not a function template specialization and F2 is a function template specialization, or, if not that,
— [...]

这就是为什么它在构造函数上调用隐式(然后是显式)副本构造函数的原因模板。

That's why it calls your implicit (and then, your explicit) copy constructor over your constructor template.

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

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