C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::atomic<bool> trivial constructor/destructor dropped in C++17 spec wording

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Sat, 17 Jun 2023 02:44:32 +0300
On 6/17/23 00:28, Jeremy Hurwitz via Std-Proposals wrote:
> Hi all,
>
> We noticed that the C++11 spec guarantees that std::atomic<bool> has a
> trivial constructor and a trivial destructor, but these three guarantees
> were lost in the C++17 spec and onward.
>
> ----------
> C++11:
> (§29.5) 4. … There shall be a specialization atomic<bool> which
> provides the general atomic operations as specified in 29.6.1.
> (§29.5) 5. The atomic integral specializations and the
> specialization atomic<bool> shall have standard layout. They shall each
> have a trivial default constructor and a trivial destructor. They shall
> each support aggregate initialization syntax.
> ----------
> C++17:
> (§32.6) 2. The specialization atomic<bool> is a standard-layout
> struct.
> (§32.6.2 ) 1. … [ Note: For the specialization atomic<bool>, see
> 32.6. — end note ]
> ----------
>
> The change occurred in P0558
> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0558r1.pdf>,
> and as far as we can tell, was not intentional. A brief experiment
> using godbolt.org <https://godbolt.org/z/EGqrrn7h7> shows that
> std::atomic<bool> is indeed trivially constructible and trivially
> destructible on the current versions of all listed compilers.
>
> Is this a defect in the current spec? Thanks!

Since C++17, std::atomic value-initializes its contained value, see:

http://eel.is/c++draft/atomics#types.operations-2

Therefore it cannot be trivially-constructible. std::atomic<bool> is not
special in this regard.

Received on 2023-06-16 23:44:35