c++language-lawyerstdatomictrivially-copyable

Is std::atomic<bool> trivially copyable?


The following code prints whether std::atomic<bool> is trivially copyable:

#include <atomic>
#include <iostream>
#include <type_traits>

int main(){
    std::cout << std::is_trivially_copyable_v<std::atomic<bool>> << "\n";
}

It gives the following result on gcc and clang:

1

demo (gcc+clang)

But on MSVC the result is:

0

The behavior of all 3 compilers is also demonstrated here using static_assert.

I thought that trivial copyablity is defined either way by the standard.
Which compiler is right (or is it implementation specific) ?


Solution

  • See also: Is a class with deleted copy-constructor trivially copyable?

    A deleted constructor is not non-trivial.

    Prior to CWG1734's resolution, to be trivially copyable all copy/move constructors/assign operators needed to be trivial. This is the case for std::atomic<bool> (they are all deleted)

    With the new wording, one also had to be non-deleted (and in C++20, 'eligible', as in its constraints are satisfied).

    GCC and Clang do not implement this. See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96288 https://github.com/llvm/llvm-project/issues/38398