Consider the following minimal working example:
#include <atomic>
int main() {
::std::atomic<bool> a = false;
}
Copy ctor and copy assignment of atomic are both explicitly deleted. However, this should invoke the ctor taking exactly a bool.
Both g++ and clang++ complain that this line is attempting to invoke the copy ctor of atomic
:
$ g++ -std=c++1z a.cpp
a.cpp: In function ‘int main()’:
a.cpp:4:27: error: use of deleted function ‘std::atomic<bool>::atomic(const std::atomic<bool>&)’
::std::atomic<bool> a = false;
^~~~~
$ clang++ -std=c++1z a.cpp
a.cpp:4:23: error: copying variable of type '::std::atomic<bool>' invokes deleted constructor
::std::atomic<bool> a = false;
^ ~~~~~
Why are they trying to copy an atomic
?
::std::atomic<bool> a = ::std::atomic<bool>(false);
, It creates a new std::atomic<bool>(false), then copies it. But, when something like this is done::std::atomic<bool> a ( false );
, it works. Can a compiler guarantee copy elision, I think C++17 guarantees it ?? – Phyto