int main()
{
auto l = [x = 10]() -> decltype(x) {};
}
clang++ 4.0 rejects this code,錯誤如下:訪問概括捕獲在拉姆達對象後返回類型
error: use of undeclared identifier 'x' auto l = [x = 10]() -> decltype(x) {}; ^
g++ 7 rejects this code,錯誤如下:
In function 'int main()': error: 'x' was not declared in this scope auto l = [x = 10]() -> decltype(x) {}; ^ error: 'x' was not declared in this scope In lambda function: warning: no return statement in function returning non-void [-Wreturn-type] auto l = [x = 10]() -> decltype(x) {}; ^
這是一個錯誤還是標準中有某些東西可以顯式地阻止在lambda的尾隨返回類型中使用用C++ 14通用語法捕獲的對象?
注意,兩種編譯器很高興與非廣義捕獲:
int main()
{
int x = 10;
auto l = [x]() -> decltype(x) { return 0; };
}
'int'曾經是一個默認的返回類型,gcc會推導出其他類型嗎? – alexeykuzmin0
@ alexeykuzmin0:很好。 [它始終「推斷」'int'](http://melpon.org/wandbox/permlink/OivD8IYUT3Jq0720)...更新問題 –
有一些奇怪的例子,例如decltype和lambda, [本](https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/6-VL5bzK6Ik)。 – TartanLlama