constexpr static data member giving undefined reference error

后端 未结 1 1777
梦毁少年i
梦毁少年i 2021-01-14 12:34

I\'m working on a kernel and I want to make my static data member constexpr so I can have its values in an enum class. However, if I do so I get an

1条回答
  •  独厮守ぢ
    2021-01-14 13:05

    A static data member shall be defined outside the class if it is odr-used---period. There are no exceptions to this rule.

    In the case that the data member is constexpr, it must also be initialized inside the class definition. However this does not abrogate the requirement to define it outside the class.

    Correct code:

    class Terminal
    {
        constexpr static uint32_t col_map[16] = { /* ... */ };
        // ...
    };
    constexpr uint32_t Terminal::col_map[16]; // definition
    

    It's counterintuitive, but that's just the way it is.

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