此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;
};
然后程序运行而不会崩溃。
此代码或编译器/STD库错误是问题吗?
我尚未使用较新版本的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