【发布时间】:2016-12-28 07:24:01
【问题描述】:
假设我有一个类型:
struct my_type
{
double operator()(int a)
{
return 3.1415;
}
};
然后我想把它包装在std::function 中。考虑两种不同的方法:
my_type m_t;
std::function<double(int)> f(std::move(m_t));
std::cout << f(4) << std::endl;
一切正常,如我所料,PI 的第一个数字被打印出来。那么第二种方法:
std::function<double(int)> ff(my_type());
std::cout << ff(4) << std::endl;
在我看来,这段代码绝对与第一个相同。 rvalue 作为 function 包装器的参数传递。但问题是,第二个代码无法编译!我真的不知道为什么会这样。
【问题讨论】:
标签: c++ function c++11 stl most-vexing-parse