Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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::boolalpha;
    std::cout << std::is_trivially_copyable_v<std::atomic<bool>> << "\n";
}

It gives the following result on gcc (v15.2) and clang (v21.1):

true

But on MSVC (v19 latest) the result is:

false

Live demo - Godbolt

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) ?

like image 222
wohlstad Avatar asked Jan 26 '26 23:01

wohlstad


1 Answers

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

like image 108
Artyer Avatar answered Jan 28 '26 12:01

Artyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!