Is a template constructor overriding the implicit copy constructor in C ++?

Is a template constructor (like the following) overriding the constructor of implicit copies?

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

If so, does it override it anyway, if other is passed by value, and not by permalink?

If so, is there any way to do this without explicitly defining a copy constructor?

+7
source share
3 answers

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

A constructor without a template 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 all other parameters have default arguments.

The compiler will still implicitly generate a default.

You can make this explicit (C ++ 11 required) on

 Foo(const Foo<T>&) = default; 
+7
source

Is a template constructor (like the following) overriding the constructor of implicit copies?

Not. The copy constructor is still implicitly declared and selected in the preference over the template.

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

Not. If you do not need an implicit copy constructor, you will have to define it yourself.

+2
source

The template constructor or assignment operator, which is similar to the template constructor of the constructor constructor / copy constructor / copy / copy operator / copy assignment operator / redirection operator] is not really the default constructor / instance constructor / move constructor / copy assignment operator / move operator assignment] and will not replace it or prevent it from being implicitly created.

+2
source

All Articles