std::atomic
is not copyable or movable.
Before C++17, the copy-initialization std::atomic<int> x = 0;
would first construct a temporary std::atomic<int>
from 0
and then direct-initialize x
from that temporary. Without a move or copy constructor this would fail and so the line doesn't compile.
std::atomic<int> x(0);
however is direct-initialization and will just construct x
with the argument 0
to the constructor.
Since C++17 there is no temporary and x
will directly be initialized by a constructor call with 0
as argument in any case and so there is no issue with std::atomic
being non-movable. In that sense the slide is now out-dated.
Even though the behavior is now the same for copy-initialization and direct-initialization in this case, there are still differences between the two in general. In particular direct-initialization chooses a constructor to initialize the variable directly by overload resolution while copy-initialization tries to find an implicit conversion sequence (possibly via converting constructor or conversion function with different overload resolution rules). Also, copy-initialization, in contrast to direct-initialization, does not consider constructors marked explicit
.
Regarding the code snippet in the question. 1-h
and 1-f
are copy-initialization as above. 3-f
is direct-initialization as above. 2-h
and 2-f
are direct-list-initialization, which behaves different from both others in some cases, but here it has the same effect as direct-initialization with parentheses.
Explaining all the differences between the initialization forms in general would take a while. This is famously one of the most complex parts of C++.
T x = 0;
would always try to call a constructorT(int)
regardless of the class – Pendragon