Initializing a constexpr with a const, — int vs float

前端 未结 2 1918
情歌与酒
情歌与酒 2021-01-17 08:05

I\'m wondering why the integer ii is initiallized at compile time, but not the float ff here:

int main() {
  const int i = 1;
  con         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-17 08:19

    Constant variables of integral types with constant initializers are integral constant expressions (de facto implicitely constexpr; see expr.const in ISO C++). float is not an integral type and does not meet the requirements for constant expression without the use of constexpr. (A similar case is why int can be but float cannot be a template parameter.)

提交回复
热议问题