我想使用 C++14 中引入的广义 lambda 捕获(有关解释,请参阅 Move capture in lambda)。但是,我的代码的其余部分是 C++11 友好的。我想按照以下方式做一些事情
#ifdef CPP14
// move capture in lambda
#else
// capture by-value
#endif
但是,没有好的交叉编译器标志来推断版本。有什么可以建议的吗? (当然,除了定义我自己的宏)
最佳答案
实际上 T.C.是的,C++11 FDIS 在 "16.8 Predefined macro names [cpp.predefined]" 中说那个
The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit.
脚注指出:
It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming com- pilers should use a value with at most five decimal digits.
所以使用下面的代码对我来说似乎完全合法。
#if __cplusplus > 201103L
//c++1y or above
#else
//c++11 or below
#endif
但是,某些编译器可能不遵循标准,您可能需要检查 _cplusplus
值是否已针对 c++1y 递增。
例如,在 4.7.0 版之前,GCC 将此标志设置为 1。
如果您需要有关 _cplusplus
标志的更多信息,您应该查看 this question
关于c++ - 广义 lambda 捕获的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24622970/