It’s because the initializer is in the declaration, not the definition, it doesn’t qualify as a constant integral expression.
If it was like this, it would work:
class Foo
{
public:
static const int a = 1;
static const int b = 2;
static const int c = 3;
};
int main()
{
int v = 2;
switch (v)
{
case Foo::a: break;
case Foo::b: break;
case Foo::c: break;
}
}
const int Foo::a;
const int Foo::b;
const int Foo::c;
But since it is like this, it doesn’t:
class Foo
{
public:
static const int a;
static const int b;
static const int c;
};
int main()
{
int v = 2;
switch (v)
{
case Foo::a: break;
case Foo::b: break;
case Foo::c: break;
}
}
const int Foo::a = 1;
const int Foo::b = 2;
const int Foo::c = 3;