Before C++11, we can do copy initialization by writing something like A a = 1;
which is more or less equivalent to A a = A(1);
. That is, a temporary is first created and then a copy ctor is invoked. Regardless of copy elision, this must be so conceptually and the copy ctor must be accessible.
With list initialization in C++11, we can do a copy list initialization by writing A a = {1, 2};
. In my opinion, this should be more or less equivalent to A a = A(1, 2);
. However, on GCC and clang, A a = {1, 2}
compiles even when the copy and move ctor are inaccessible (by declaring as private). Still, A a = 1;
does not compile on GCC or clang if the corresponding copy/move ctor is inaccessible. So, A a = {1, 2};
seems more or less equivalent to A a{1, 2};
which is direct list initialization. The difference between this and the real direct list initialization is that A a = {1, 2};
does not compile if the ctor that takes two ints are explicit. In this aspect, A a = {1, 2};
resembles copy initialization.
So, my question is: what is the exact semantics of expressions like A a = {1, 2};
conceptually? By conceptually, copy elision do not stay in the way.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…