Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][c++17] const static inline variables initialized in wrong order on Windows #110975

Open
mprettner-huawei opened this issue Oct 3, 2024 · 1 comment
Labels
clang Clang issues not falling into any other category

Comments

@mprettner-huawei
Copy link

The following code produces a non-intuitive output when running with Clang19 on Windows:

#include <iostream>

struct A
{
    int m_value;

    A(int value) : m_value(value)
    {
        std::cout << "A::A(" << value << ")\n";
    }

    int GetValue() const
    {
        std::cout << "A::GetValue() -> " << m_value << "\n";
        return m_value;
    }
};

struct B
{
    const static inline A a = A(1);
    const static inline int b = a.GetValue();
};

int main()
{
    std::cout << "Result: " << B::b << "\n";
    return 0;
}

The following output is generated:

A::GetValue() -> 0
A::A(1)
Result: 0

According to my understanding of the C++ standard this code should be correct and the output should be "A::A, A::GetValue, Result", but with Clang19 on Windows the order of GetVaule and the constructor is swapped, which leads to the wrong result. The same code works correctly with Clang on Linux and MSVC on Windows.

Platform:

  • Windows 10 22H2
  • Clang installer from Github (LLVM-19.1.0-win64.exe)
  • Visual Studio build tools 2022 (17.9.6)
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Oct 3, 2024
@mprettner-huawei mprettner-huawei changed the title [clang][c++17] const static inline variable initialization order [clang][c++17] const static inline variables initialized in wrong order on Windows Oct 3, 2024
@Disservin
Copy link

can reproduce with clang 16.0.5 as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

2 participants