I'm a little bit confused about the following code:
struct A {
std::atomic<int> a = 0;
};
Which gives an error:
copying member subobject of type 'std::atomic' invokes deleted constructor
But almost the same code does work:
struct A {
std::atomic<int> a = {0};
};
Okey, if the first variant requires the copy constructor, then it have to use operator=()
. But wait! This operator perfectly works without the copy constructor:
A a;
a.a = 1;
Can anyone explain how both of the in-place initializations are expanded in terms of simple operations? Why the first one requires copy constructor?
All references are to N3797, the C++1y current working draft. §8.5 Initializers [dcl.init]/15 states:
The initialization that occurs in the form
T x = a;
as well as in argument passing, function return, throwing an exception (15.1), handling an exception (15.3), and aggregate member initialization (8.5.1) is called copy-initialization. [ Note: Copy-initialization may invoke a move (12.8). —end note ]
So the declaration:
std::atomic<int> a = 0;
is performing copy-initialization. According to 8.5/17:
The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.
The destination type here is std::atomic<int>
and the source type is int
(i.e., decltype(0)
). To determine the semantics of the initialization, we have to determine which of paragraph 17's bullets applies:
- If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
- If the destination type is a reference type, see 8.5.3.
- If the destination type is an array of characters, an array of
char16_t
, an array ofchar32_t
, or an array ofwchar_t
, and the initializer is a string literal, see 8.5.2.- If the initializer is
()
, the object is value-initialized.- Otherwise, if the destination type is an array, the program is ill-formed.
- If the destination type is a (possibly cv-qualified) class type:
- If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, ... [does not apply, source type is
int
]- Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is a prvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.
- ...
There we are. The initializer expression - 0
- is converted into a std::atomic<int>
via the creation of a temporary object initialized with the std::atomic<int>(int)
constructor. That temporary object is used to direct-initialize the original std::atomic<int>
object. The other of the "(possibly cv-qualified) class type" bullets that we ignored before now applies:
Recall that the new initializer is a prvalue std::atomic<int>
. Overload resolution determines that there is no appropriate std::atomic<int>
constructor that accepts a single argument std::atomic<int>&&
(std::atomic<int>
is not movable or copyable) and diagnoses the program as ill-formed.
For the second part of the question,
std::atomic<int> a = {0};
is again copy initialization per 8.5/15. This time, however, the very first bullet of 8.5/17 applies:
- If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
For list-initialization, we must look to 8.5.4/3:
List-initialization of an object or reference of type
T
is defined as follows:
- If
T
is an aggregate, aggregate initialization is performed (8.5.1).- Otherwise, if the initializer list has no elements and
T
is a class type with a default constructor, the object is value-initialized.- Otherwise, if
T
is a specialization ofstd::initializer_list<E>
, a prvalueinitializer_list
object is constructed as described below and used to initialize the object according to the rules for initialization of an object from a class of the same type (8.5).- Otherwise, if
T
is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution (13.3, 13.3.1.7). If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.- ...
std::atomic<int>
is a class type, not an aggregate or initializer_list
specialization, so constructors are considered. The std::atomic<int>::atomic(int)
constructor will be selected as a perfect match and is used to initialize the object.