std::atomic
variable (since it is not copyable or movable) can only be initialized using direct-initialization.
std::atomic_int turnX = 5;
will only compile and run on a C++17 compiler or higher. Explanation:
// Prior to C++17
std::atomic_int turnX = 5; // Copy initialization => Compilation error.
std::atomic_int turnX(5); // Direct initialization.
std::atomic_int turnX{ 5 }; // Direct initialization.
Prior to C++17, the copy-initialization std::atomic_int turnX = 5;
would first construct a temporary std::atomic_int
from 5 and then direct-initialize turnX
from that temporary. Without a move or copy constructor this would fail and so the line doesn't compile. std::atomic_int turnX (5);
or std::atomic_int turnX {5};
however, is directly initialized by a constructor call with 5 as argument.
// C++17 or higher
std::atomic_int turnX = 5; // Direct initialization.
std::atomic_int turnX(5); // Direct initialization.
std::atomic_int turnX{ 5 }; // Direct initialization.
In C++17 or higher, the temporary is no longer created. Instead, turnX
is directly initialized by a constructor call with 5 as argument. So, there is no issue with std::atomic_int
being not copyable or movable.