Skip to content

Aggregates containing elements with constexpr constructors are not statically initalized #44712

@BillyONeal

Description

@BillyONeal
Bugzilla Link 45367
Version 10.0
OS Windows NT
Attachments repro.cpp
CC @efriedma-quic,@saxbophone,@zygoloid,@rnk,@yurybura

Extended Description

Previously reported as microsoft/STL#661

In the following example, should_be_statically_initalized should be eligible for static initialization, then the dynamic initializer for foo that calls bar() should run, resulting in the asserts in main(). However, clang only seems to give these static initializers if the varaible is {}'d.

C:\Users\billy\Desktop>type repro.cpp
#include <assert.h>

struct FakeAtomic {
    int example;

    constexpr FakeAtomic() noexcept : example{} {}
    int operator++() { return ++example; }
    int operator++(int) { return example++; }
    int load() const { return example; }
};

struct Meow {
    FakeAtomic data[2];
    char padding[64];
};

int bar();

int foo = bar();
#ifdef WORKAROUND
Meow should_be_statically_initalized[256]{};
#else
Meow should_be_statically_initalized[256];
#endif

int bar() { return should_be_statically_initalized[0].data[0]++; }

int main() {
    assert(foo == 0);
    assert(should_be_statically_initalized[0].data[0].load() == 1);
}
C:\Users\billy\Desktop>clang-cl /EHsc /W4 /WX .\repro.cpp

C:\Users\billy\Desktop>.\repro.exe
Assertion failed: should_be_statically_initalized[0].data[0].load() == 1, file .\repro.cpp, line 30

C:\Users\billy\Desktop>clang-cl /DWORKAROUND /EHsc /W4 /WX .\repro.cpp

C:\Users\billy\Desktop>.\repro.exe

C:\Users\billy\Desktop>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++20clang:to-be-triagedShould not be used for new issueswaiting-for-wg21Blocked on C++ Standards Committee

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions