Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the standard consider a template constructor as a copy constructor?

Here's the definition of copy constructor, [class.copy.ctor/1]:

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 ([dcl.fct.default]).

Why does the standard exclude templates as copy constructors?

In this simple example, both constructors are copy constructors:

struct Foo {     Foo(const Foo &); // copy constructor     Foo(Foo &);       // copy constructor }; 

See this similar example:

struct Foo {      Foo() = default;       template <typename T>      Foo(T &) {          printf("here\n");      } };  int main() {     Foo a;     Foo b = a; } 

In this example, here will be printed. So it seems that my template constructor is a copy constructor, at least it behaves like one (it is called in a context where copy constructors are usually called).

Why is the "non-template" requirement there in the text?

like image 296
geza Avatar asked Apr 25 '19 09:04

geza


People also ask

Can copy constructor be template?

The copy constructor lets you create a new object from an existing one by initialization. A copy constructor of a class A is a non-template constructor in which the first parameter is of type A& , const A& , volatile A& , or const volatile A& , and the rest of its parameters (if there are any) have default values.

Why is my copy constructor not being called?

The reason the copy constructor is not called is because the copy constructor itself is a function with one parameter. You didn't call such function,so it didn't execute.

Why there is no copy constructor in Java?

In C++ that statement makes a copy of the object's state. In Java it simply copies the reference. The object's state is not copied so implicitly calling the copy constructor makes no sense. And that's all there is to it really.

How is a copy constructor different from a constructor?

Constructor: It is a method which has the same name as the class which is used to create an instance of the class. Copy Constructor: Used to create an object by copying variables from another object of the same class. The main purpose is to create a new object from an existing one by copying variables.


1 Answers

Let's put templates aside for a second. If a class doesn't declare a copy constructor, an implicitly defaulted one is generated. It may be defined as deleted, but it's defaulted nonetheless.

A member template is not a member function. Members are instantiated from it only when needed.

So how can a compiler know from the class definition alone whether or not a specialization with T = Foo will ever be needed? It can't. But it's exactly that on which it needs to base a decision of how to handle a potential need for an implicitly defaulted copy constructor (AND move constructor). That becomes messy.

The easiest approach is to exclude templates. We'll always have some copy constructor anyway, it will do the correct thingTM by default, and will be favored by overload resolution because it's not instantiated from a template.

like image 178
StoryTeller - Unslander Monica Avatar answered Oct 10 '22 17:10

StoryTeller - Unslander Monica