lambda中的lambda不能做模板?
Lambda in lambda cannot be a template?
Visual Studio 编译器 (MSVC 2015) 无法编译以下简单代码:
int main() {
auto foo = [](auto callback) {
callback(int{});
};
auto rexs = [&foo]() {
foo([](auto tg) {});
};
}
它吐出一个内部编译器错误:
fatal error C1001: An internal error has occurred in the compiler.
VC++ 喜欢在编译代码包含错误时给出错误 C1001
(即程序员犯了一个错误,但 VC++ 只是没有完全识别代码中的错误),所以我想知道我是否在这里犯了错误。
然而,从我能看到的所有角度来看,我的代码看起来都符合标准,而且对我来说它似乎是一个 VC++ 错误。我的想法对吗?
您的代码在 clang 3.8 和 gcc 5.4 (http://rextester.com/SCAH69935) 下编译良好,因此它似乎是一个 VC++ 错误。
VC++
是正确的:C++11 不允许在 lambda 中使用 auto
。
自 C++14 起,我们只能在 lambda 中使用 auto
。
Clang 3.8.0 给我这个错误 -std=c++11
:
main.cpp:7:17: error: 'auto' not allowed in lambda parameter
auto f = [](auto x) { return x + 1;};
和 gcc 7.1.0(具有相同的标志)给了我这个:
main.cpp: 在函数中 'int main()':
main.cpp:7:17: error: use of 'auto' in lambda parameter declaration only available with -std=c++14 or -std=gnu++14
auto f = [](auto x) { return x + 1;};
对于此代码:
int main()
{
auto f = [](auto x) { return x + 1;};
}
Visual Studio 编译器 (MSVC 2015) 无法编译以下简单代码:
int main() {
auto foo = [](auto callback) {
callback(int{});
};
auto rexs = [&foo]() {
foo([](auto tg) {});
};
}
它吐出一个内部编译器错误:
fatal error C1001: An internal error has occurred in the compiler.
VC++ 喜欢在编译代码包含错误时给出错误 C1001
(即程序员犯了一个错误,但 VC++ 只是没有完全识别代码中的错误),所以我想知道我是否在这里犯了错误。
然而,从我能看到的所有角度来看,我的代码看起来都符合标准,而且对我来说它似乎是一个 VC++ 错误。我的想法对吗?
您的代码在 clang 3.8 和 gcc 5.4 (http://rextester.com/SCAH69935) 下编译良好,因此它似乎是一个 VC++ 错误。
VC++
是正确的:C++11 不允许在 lambda 中使用 auto
。
自 C++14 起,我们只能在 lambda 中使用 auto
。
Clang 3.8.0 给我这个错误 -std=c++11
:
main.cpp:7:17: error: 'auto' not allowed in lambda parameter
auto f = [](auto x) { return x + 1;};
和 gcc 7.1.0(具有相同的标志)给了我这个: main.cpp: 在函数中 'int main()':
main.cpp:7:17: error: use of 'auto' in lambda parameter declaration only available with -std=c++14 or -std=gnu++14
auto f = [](auto x) { return x + 1;};
对于此代码:
int main()
{
auto f = [](auto x) { return x + 1;};
}