Omn*_*ity 8 c++ lambda g++ c++11 trailing-return-type
我一直在编写代码,我最近发现g ++没有警告我某类问题:根据C++ 11 5.1.2.4,如果你的lambda不是单个return语句那么返回类型必须声明为尾随返回类型或无效.
虽然g ++被允许编译无效的代码,如果它有足够的意义,是否有办法关闭此行为(允许-fpedantic
在g ++ - 4.7中)或至少警告它?
示例代码:
[]() { return 0; } //is fine
[&a]() { a++; return 0; } //is not fine but g++ doesn't warn me
[&a]() -> int {a++; return 0; } //is fine again
Run Code Online (Sandbox Code Playgroud)
C++ 11 5.1.2.4
实现不应将rvalue引用类型的成员添加到闭包类型.如果lambda表达式不包含lambda声明符,那就好像lambda声明符是().如果lambda表达式不包含trailing-return-type,则就好像trailing-return-type表示以下类型:
- 如果compound-statement的格式为
{attribute-specifier-seq(opt)返回表达式; }
返回的表达的左值到右值转换(4.1),阵列到指针转换(4.2),和功能到指针转换(4.3)后的类型;- 否则,无效.