msvc std::function doesn't accept generic lambda with a nested lambda
The following works with gcc 5.2 and clang 3.7 but fails with msvc 2015:
#include <functional>
int main()
{
auto const foo = [](auto&& i) {
auto const bar = []{ return 100; };
return bar();
};
std::function<int(int)> fn = foo;
return 0;
}
Is it a bug in msvc or are gcc and clang too lax?
If I try this using Microsoft's official online compiler, which is version 19.00.23602.0(x86) last updated Dec 3, 2015, I get execution timed out1. Removing std::function<int(int)> fn = foo;
will allow successful compilation. Feel free to let Microsoft know via Visual Studio Connect so they can investigate the bug and report it. Yes, one can waste time flipping through the standard to see if this is "legal" code, but you'll get much more bang for your buck by using the bug tracker.
1 An internal compiler error is always a bug.
This is a bug in Visual C++ 2015, but it seems to have been fixed as of Update 3. The workaround is to specify the return type of the outer lambda (thanks to melak47).
This fails:
#include <functional>
int main()
{
std::function<void (bool)> f =
[](auto&&)
{
[]{};
};
}
with:
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 Copyright (C) Microsoft Corporation. All rights reserved. a.cpp C:Program Files (x86)Microsoft Visual Studio 14.0VCINCLUDEtype_traits(1348): error C2065: '_Args': undeclared identifier [...and more]
But this works:
#include <functional>
int main()
{
std::function<void (bool)> f =
[](auto&&) -> void
{
[]{};
};
}
链接地址: http://www.djcxy.com/p/72530.html
上一篇: MSVC中嵌套类型名称的冲突