相关疑难解决方法(0)

使用const初始化constexpr, - int vs float

我想知道为什么整数ii在编译时初始化,但不是浮点数ff:

int main() {
  const int i = 1;
  constexpr int ii = i;

  const float f = 1.0;
  constexpr float ff = f;
 }
Run Code Online (Sandbox Code Playgroud)

这是我尝试编译时会发生的事情:

> g++ -std=c++11 test.cc
test.cc: In function ‘int main()’:
test.cc:6:24: error: the value of ‘f’ is not usable in a constant expression
   constexpr float ff = f;
                        ^
test.cc:5:15: note: ‘f’ was not declared ‘constexpr’
   const float f = 1.0;
Run Code Online (Sandbox Code Playgroud)

c++ constexpr

23
推荐指数
2
解决办法
2205
查看次数

标签 统计

c++ ×1

constexpr ×1