模板化构造函数是否覆盖C中的隐式复制构造函数?

前端之家收集整理的这篇文章主要介绍了模板化构造函数是否覆盖C中的隐式复制构造函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
模板化构造函数(如下所示)是否会覆盖隐式复制构造函数
template <class T>
struct Foo
{
    T data;

    // ...

    template <class U>
    Foo(const Foo<U> &other) : data((T)doSomethingWith(other.data)) {}

    // ...
};

如果是这样,如果通过值而不是常量引用传递其他值,它是否仍会覆盖它?

如果是这样,有没有明确定义复制构造函数方法呢?

解决方法

@H_404_11@ 不,那不是复制构造函数.本标准第12.8节([class.copy])要求:

A non-template constructor for class X is a copy constructor if its first parameter is of type X&,const X&,volatile X& or const volatile X&,and either there are no other parameters or else all other parameters have default arguments.

编译器仍将隐式生成默认值.

你可以明确(需要C 11)

Foo(const Foo<T>&) = default;
原文链接:https://www.f2er.com/c/118617.html

猜你在找的C&C++相关文章