Does a templated constructor override the implicit copy constructor in C++?

情到浓时终转凉″ 提交于 2019-12-01 17:38:55

No, that is not a copy constructor. Section 12.8 ([class.copy]) of the Standard requires that:

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.

The compiler will still implicitly generate a defaulted one.

You can make that explicit (requires C++11) by

Foo(const Foo<T>&) = default;

Does a templated constructor (such as the following) override the implicit copy constructor?

No. The copy constructor is still implicitly declared, and is chosen in preference to the template.

Is there any way around this without explicitly defining a copy constructor?

No. If you don't want the implicit copy constructor, then you'll have to define one yourself.

A templated constructor or assignment operator which looks like a templated [default constructor/copy constructor/move constructor/copy assignment operator/move assignment operator] is not really a [default constructor/copy constructor/move constructor/copy assignment operator/move assignment operator] and will not replace it or prevent it from being implicitly generated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!