【发布时间】:2015-05-11 09:42:26
【问题描述】:
据我know,在标准 C++11(不是 C++14)中,当省略 lambda 的返回类型时,其返回类型推断为:
- 返回表达式的类型,只要 lambda 只包含一个带有表达式的 return 语句,或者
-
void在所有其他情况下。
现在考虑这段代码:
#include <iostream>
auto closure = [](int x)
{
x++;
return x;
};
int main()
{
int y = closure(10);
std::cout << y << std::endl;
}
这应该属于情况 2。但是,在 g++4.9.2、g++5 和 clang++ 中,代码编译就像是带有 auto 类型推导的 C++14,带有 -pedantic -Wall -Wextra -std=c++11。这里发生了什么?我是不是对标准的解释错了?
【问题讨论】:
标签: c++ c++11 lambda type-deduction