I could not find where in the standard it is stated that it is forbidden to explicitly default a copy-constructor and copy-assignment with a volatile&
or const volatile&
argument, like this:
struct A{
A(const volatile A&) =default; // fails to compile on (all) compilers
};
In [dcl.fct.def.default] there is no such a restriction, while [class.copy] specifies that A(const volatile A&)
is a copy constructor.
Note: I am just looking for the location in the text of the standard which specifies this behavior.
When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference. One reason for passing const reference is, we should use const in C++ wherever possible so that objects are not accidentally modified.
If the argument is passed by value, its copy constructor would call itself to copy the actual parameter to formal parameter. This process would go on until the system runs out of memory. So, we should pass it by reference , so that copy constructor does not get invoked. Save this answer.
Constructors are used to create, and can initialize, objects of their class type. You cannot declare a constructor as virtual or static , nor can you declare a constructor as const , volatile , or const volatile .
A copy constructor has as its first parameter a (possibly const or volatile) reference to its own class type. It can have more arguments, but the rest must have default values associated with them.
You are in the right sections, but are overlooking some crucial bullets.
[dcl.fct.def.default]/1:
A function definition of the form:
...
is called an explicitly-defaulted definition. A function that is explicitly defaulted shall
- have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be “reference to non-const T”, where T is the name of the member function's class) as if it had been implicitly declared, and
[class.copy.ctor]/7:
The implicitly-declared copy constructor for a class X will have the form
X::X(const X&)
if each potentially constructed subobject of a class type M (or array thereof) has a copy constructor whose first parameter is of type const M& or const volatile M&.119 Otherwise, the implicitly-declared copy constructor will have the form
X::X(X&)
...
119) This implies that the reference parameter of the implicitly-declared copy constructor cannot bind to a volatile lvalue;
When the above is summed up, your only two options for explicitly defaulting a copy c'tor are these:
struct A {
A(const A&) = default;
};
struct B {
B(B&) = default;
};
When the standard says A(const volatile A&)
is a copy constructor. It means that a user-provided c'tor with such a parameter can be the classes copy c'tor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With