Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I not provided with a default copy constructor from a volatile?

Tags:

This code:

class X {   int member;   };  volatile X a; X b = a; 

Fails with the error:

prog.cpp:6:7: error: no matching function for call to ‘X::X(volatile X&)’ prog.cpp:6:7: note: candidates are: prog.cpp:1:7: note: X::X() prog.cpp:1:7: note:   candidate expects 0 arguments, 1 provided prog.cpp:1:7: note: X::X(const X&) prog.cpp:1:7: note:   no known conversion for argument 1 from ‘volatile X’ to ‘const X&’ 

Is there any way I can get the compiler to generate a volatile copy constructor for me?

like image 874
Eric Avatar asked Jun 20 '13 15:06

Eric


People also ask

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.

Is copy constructor created automatically?

No copy constructor is automatically generated.

Does Java not provide default copy constructor?

No, it doesn't have a default copy constructor. A default constructor. You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors.

Can a constructor be volatile?

You cannot declare a constructor as virtual or static , nor can you declare a constructor as const , volatile , or const volatile .


1 Answers

The short answer is: Because the standard says you won't.

The C++ Standard 12.8/9 (Draft N3242) tells:

The implicitly-declared copy constructor for a class X will have the form

  • X::X(const X&)

if

  • each direct or virtual base class B of X has a copy constructor whose first parameter is of type const B& or const volatile B&, and
  • for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a copy constructor whose first parameter is of type const M& or const volatile M&. [Note: 119]

Otherwise, the implicitly-declared copy constructor will have the form

  • X::X(X&)

Note 119 says:

This implies that the reference parameter of the implicitly-declared copy constructor cannot bind to a volatile lvalue; see C.1.9.

In C.1.9 you'll find:

The implicitly-declared copy constructor and implicitly-declared copy assignment operator cannot make a copy of a volatile lvalue. For example, the following is valid in ISO C:

struct X { int i; }; volatile struct X x1 = {0}; struct X x2(x1); // invalid C++ struct X x3; x3 = x1; // also invalid C++ 

Rationale: Several alternatives were debated at length. Changing the parameter to volatile const X& would greatly complicate the generation of efficient code for class objects. Discussion of providing two alternative signatures for these implicitly-defined operations raised unanswered concerns about creating ambiguities and complicating the rules that specify the formation of these operators according to the bases and members.

like image 178
Pixelchemist Avatar answered Sep 25 '22 02:09

Pixelchemist