Why doesn't gcc allow a const int as a case expression?

后端 未结 1 582
野趣味
野趣味 2020-12-15 03:37

I was looking at this SO question and got to thinking about const ints versus #defines and realized I don\'t actually understand why the compiler can\'t deal with this. Coul

相关标签:
1条回答
  • 2020-12-15 04:38

    A constant expression is not the same as a const-qualified type value, even though technically the value is known by the compiler at the point of the case statement.

    Imagine what would happen if another file declared extern const int FOO and tried to use it the same way. The compiler wouldn't know what FOO was because it was defined in another file. Even though it has a constant value, it is not a constant expression.

    0 讨论(0)
提交回复
热议问题