c – 广义lambda捕获的宏

我想使用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.

例如,GCC将此标志设置为1,直到版本4.7.0.

如果您需要有关_cplusplus标志的更多信息,请查看this question

点赞