0

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?

4
  • You need to add constexpr for this code to be valid. Commented Jun 18, 2014 at 6:37
  • The C++ standard allows only static constant integral or enumeration types to be initialized inside the class. Commented Jun 18, 2014 at 6:38
  • Sorry for the duplicate.
    – user3235674
    Commented Jun 18, 2014 at 6:43
  • Nothing to be sorry for :) Commented Jun 18, 2014 at 6:44

1 Answer 1

4

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" };