c++ - 广义 lambda 捕获的宏

标签 c++ c++11 lambda c++14

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

相关文章:

c++ - 如何通过 TCP 发送 10,000 ~ 20,000 字节的数据?

c++ - 如何将 std::function 分配给 C++ 中的运算符?

Java - 通过参数传递方法

c# - 如何使用 Linq 或 Lambda 执行更新?(C#, Asp.net,Linq , Lambda)

c++ - 静态函数数组上未解析的外部符号

c++ - OpenCV 检测多个、旋转、缩放的对象

c++ - int2、int3、float2、float3 等类型呢?

c++ - 来自 stdint.h 的快速类型的溢出行为

c++ - 检查父构造函数是否有参数

lambda - 您可以使用 Lambda 在通过 Cloudformation 创建的 RDS 上创建架构吗?