Search code examples
c++arraysvisual-studio-2013intellisenseconst-char

IntelliSense: a member of type "const char *const []" cannot have an in-class initializer


Consider:

class Note
{
public: 
    // ...
private:
    static const char* const NOTE_NAMES[12] =
            { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
}

While it compiles fine, IntelliSense gave me an error:

IntelliSense: a member of type "const char *const [12]" cannot have an in-class initializer

Is this a bug or am I doing something wrong?


Solution

  • According to the C++ Standard

    If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignmentexpression is a constant expression

    So your code does not satisfy the C++ Standard.

    On the other hand

    A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression

    So to get the valid code you should write

    static constexpr char* const NOTE_NAMES[12] =
            { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };