小编Cyd*_*uzo的帖子

C++ 11,如何在 #if 中使用 const

我在我的代码中使用了一个 const 变量。并想告诉我的预处理器使用它。即:

const double x = 1.2;
const bool xIsZero = x==0;

#if xIsZero
...
#endif
Run Code Online (Sandbox Code Playgroud)

但这不起作用。在 C++ 17 中,if constexpr没有诀窍。但我现在坚持使用 C++11。

因此,我可以使用以下解决方法:

#define X 1.2
#define xIsZero (x==0)
const double x = X;

#if xIsZero
...
#endif
Run Code Online (Sandbox Code Playgroud)

但我只是不喜欢将 x 赋予 #define,我想直接将其赋予常量。有没有办法做到这一点?

c++ if-statement constants

6
推荐指数
1
解决办法
350
查看次数

标签 统计

c++ ×1

constants ×1

if-statement ×1