从 VS 2012 中的 lambda 返回值构造 std::function 时崩溃
Crash when std::function is constructed from lambda returned value in VS 2012
此 C++ 代码使用 VS 2012 成功编译但在运行时崩溃:
#include <iostream>
#include <functional>
void f()
{
std::cout << "f called" << std::endl;
}
int main()
{
auto get_f= []()
{
bool b = true;
return b ? f : f;
};
std::function<void()> filter(get_f()); // crash here!!!
return 0;
}
如果我们将 get_f 更改为:
auto get_f= []()
{
return f;
};
然后程序运行没有崩溃。
是这段代码的问题还是compiler/std库的bug?
我还没有用更新版本的 Visual Studio 进行测试。
在我看来这像是标准库(或可能是编译器)的问题。
用VS 2013编译运行没有问题。如果我们添加代码来调用同样运行的 filter
:
#include <iostream>
#include <functional>
void f()
{
std::cout << "f called" << std::endl;
}
int main()
{
auto get_f= []()
{
bool b = true;
return b ? f : f;
};
std::function<void()> filter(get_f()); // crash here!!!
filter();
return 0;
}
输出:f called
此 C++ 代码使用 VS 2012 成功编译但在运行时崩溃:
#include <iostream>
#include <functional>
void f()
{
std::cout << "f called" << std::endl;
}
int main()
{
auto get_f= []()
{
bool b = true;
return b ? f : f;
};
std::function<void()> filter(get_f()); // crash here!!!
return 0;
}
如果我们将 get_f 更改为:
auto get_f= []()
{
return f;
};
然后程序运行没有崩溃。
是这段代码的问题还是compiler/std库的bug?
我还没有用更新版本的 Visual Studio 进行测试。
在我看来这像是标准库(或可能是编译器)的问题。
用VS 2013编译运行没有问题。如果我们添加代码来调用同样运行的 filter
:
#include <iostream>
#include <functional>
void f()
{
std::cout << "f called" << std::endl;
}
int main()
{
auto get_f= []()
{
bool b = true;
return b ? f : f;
};
std::function<void()> filter(get_f()); // crash here!!!
filter();
return 0;
}
输出:f called