I have the following declaration of some static const members
.h
class MyClass : public MyBase
{
public:
static const unsigned char sInvalid;
static const unsigned char sOutside;
static const unsigned char sInside;
//(41 more ...)
}
.cpp
const unsigned char MyClass::sInvalid = 0;
const unsigned char MyClass::sOutside = 1;
const unsigned char MyClass::sInside = 2;
//and so on
At some point I want to use those value in a switch like :
unsigned char value;
...
switch(value) {
case MyClass::sInvalid : /*Do some ;*/ break;
case MyClass::sOutside : /*Do some ;*/ break;
...
}
But I get the following compiler error: error: 'MyClass::sInvalid' cannot appear in a constant-expression.
I have read other switch-cannot-appear-constant-stuff and didn't find an answer for me since I don't get why those static const unsigned char
are not constant-expression.
I am using gcc 4.5.
1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed.
Constant static membersIf a static data member is declared constexpr, it is implicitly inline and does not need to be redeclared at namespace scope.
An instance const value can be computed in the ctor-initializer-list. A static const is set during startup initialization and remains unchanged for the rest of the program.
They mean exactly the same thing. You're free to choose whichever you think is easier to read. In C, you should place static at the start, but it's not yet required.
The problems you see are due to the fact that this
static const unsigned char sInvalid;
cannot be a compile time constant expression, since the compiler doesn't know its value. Initialize them in the header like this:
class MyClass : public MyBase
{
public:
static const unsigned char sInvalid = 0;
...
and it will work.
The values are indeed const
, but they're not compile-time constants.
A switch
condition is resolved at compile-time, not run-time. You can initialize sInvalid
to whatever value, as long as it's only once, and the switch
would never know about it until run-time.
It seems like you're better of using enum
s instead of static
constants. Besides the fact that it would work, it seems more appropriate design-wise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With