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

问题描述 投票:4回答:1

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

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

#if xIsZero
...
#endif

但是那不起作用。在C ++ 17中,if constexpr没有用。但是我暂时还停留在C ++ 11上。

因此,我可以使用类似的解决方法:

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

#if xIsZero
...
#endif

但是我只是不喜欢将x赋予#define,我想直接将其赋予const。有办法吗?

我在代码中使用了const变量。并想告诉我的预处理器使用它。即:const double x = 1.2; const bool xIsZero = x == 0; #if xIsZero ... #endif但这不起作用。在C ++中...

c++ if-statement const
1个回答
0
投票

如果条件在编译时是已知的,则可以使用重载来模拟C ++ 11中的if constexpr

© www.soinside.com 2019 - 2024. All rights reserved.