Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Constructor by Value

Assuming I have the following (invalid) code:

struct A {
  A(A) {};
};

MSVC gives me:

error C2652: 'A' : illegal copy constructor: first parameter must not be a 'A' 

Why does the compiler detect this as copy constructor, and not a regular constructor?

Chapter 12.8.2 of the C++ Standard says:

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&

I would expect that the compiler detects the above method as regular constructor, just like

struct A {
  A(B) {};
};

whereas B is another class.

Where is this behaviour defined?

like image 400
tobspr Avatar asked Dec 16 '16 12:12

tobspr


Video Answer


1 Answers

N4140 [class.copy]/6

A declaration of a constructor for a class X is ill-formed if its first parameter is of type (optionally cv-qualified) X and either there are no other parameters or else all other parameters have default arguments.

like image 195
cpplearner Avatar answered Nov 14 '22 16:11

cpplearner