Skip to content

<atomic>: In pre-C++20 mode, the constructor should be trivial #661

@zeux

Description

@zeux

Starting from VS 2019 16.6 (#336), std::atomic constructor is unconditionally initializing the value:

    constexpr atomic() noexcept(is_nothrow_default_constructible_v<_Ty>) : _Base() {}

Before this version. the constructor was trivial instead:

#ifdef __clang__ // TRANSITION, VSO-406237
    constexpr atomic() noexcept(is_nothrow_default_constructible_v<_Ty>) : _Base() {}
#else // ^^^ no workaround / workaround vvv
    atomic()                                  = default;
#endif // TRANSITION, VSO-406237

As far as I understand, this is a behavior change in C++20 standard. However, I would not expect to see this behavior without specifying /std:c++latest.

This is significant because it results in some cases where std::atomic in global scope now generates dynamic initializers. Notably, in this case:

struct Foo
{
    std::atomic<int> data[2];
};

Foo foo[2];

Before this change, the foo variable was placed into BSS with no dynamic initializer. After this change, the dynamic initializer is emitted. This is problematic because if any code in static constructors accesses the atomic, it may work on the atomic before it's initialized which means that the initialization will overwrite the value of the atomic to 0.

Thus this is a breaking change - which would be fine except that it's a silent breaking change, and I'd expect to have to opt into this change by using the latest standard version instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions