MSVC 2017 violating static initialization order within single translation unit

后端 未结 1 1548
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 06:22

MSVC 2017 Community with -std=c++17 chokes on the following example:

#include 

struct TC
{
    static TC const values[];
    static         


        
相关标签:
1条回答
  • 2021-01-21 06:59

    I isolate the same problem to this simpler example:

    #include <iostream>
    
    int values[3];
    constexpr int& v{ values[1] };
    
    int main()
    {
        std::cout << &v << ", " << &values[1] << "\n";
        return 0;
    }
    

    which, using latest MSVC 2017 Community, gives output of:

    0119D035, 0119D038
    

    The problem does not happen if constexpr is removed.

    So I think this is a compiler bug with constexpr reference initialization. The reference was initialized to &values[0] + 1 byte, not &values[1] as it should be.

    NB. If anyone is not familiar with constexpr reference definitions, see here or here. constexpr enforces that the initializer is an object with static storage duration.

    0 讨论(0)
提交回复
热议问题