GCC doesn't support simple integer constant expression?

后端 未结 3 839
既然无缘
既然无缘 2021-01-18 18:42

GCC 4.9 and 5.1 reject this simple C99 declaration at global scope. Clang accepts it.

const int a = 1, b = a; // error: initializer element is not constant
<         


        
3条回答
  •  执笔经年
    2021-01-18 19:07

    Because const doesn't make a constant expression -- it makes a variable that can't be assigned to (only initialized). You need constexpr to make a constant expression, which is only available in C++. C99 has no way of making a named constant expression (other than a macro, which is sort-of, but not really an expression at all).

提交回复
热议问题