Is a char array member initialization with a string literal constexpr?

Viewed 80

The following code compiles with gcc and clang, but not with msvc. For msvc the initialization of the char array member variable with a string literal seems to result in the implicitly generated default constructor not being constexpr according to the compiler:

struct S
{
#if 1
    char str[2] = "1";
#else
    char str[2] { 'a', '\0'};
#endif
};

int main()
{
    constexpr S s; // line 12: compiler error in msvc
    constexpr S s2{};
}

MSVC output

example.cpp
<source>(12): error C2131: expression did not evaluate to a constant
<source>(12): note: failure was caused by call of undefined function or one not declared 'constexpr'
<source>(12): note: see usage of 'S::S'
Compiler returned: 2

See godbolt.

Both using aggregate initialization for the containing object and initializing the array as array of character literals is accepted by all compilers.

Which compiler is right according to the C++ 20 standard?

0 Answers
Related