假设我有一个函数,接受一个右值引用:
void whatever (std::unique_ptr<int>&&) {
// Nothing!
}..。我将它的一个参数绑定到一个占位符。
auto f = std::bind(&whatever, _1);我尝试了这样的调用,结果与我预期的相反。
std::unique_ptr<int> nothing;
f(std::move(nothing)); // Fails to compile!
f(nothing); // Works, but seems wrong!这是一个编译器错误吗?或者,工作调用是不安全的代码吗?或者为什么我不需要将这个指针std::move到绑定的函数中?
顺便说一下,gcc4.4的编译错误是:
test.cxx:14: error: no match for call to '(std::_Bind<void (*(std::_Placeholder<1>))(std::unique_ptr<int, std::default_delete<int> >&&)>) (std::unique_ptr<int, std::default_delete<int> >)'发布于 2012-01-06 00:57:56
在使用libc++时,我得到的结果正好相反。
std::unique_ptr<int> nothing;
f(std::move(nothing)); // Works!
f(nothing); // Fails to compile!我相信这是一个gcc4.4错误。func.bind.bind/p10/b3描述了这种情况:
j的值is_placeholder<TiD>::value不为零,则参数为std::forward<Uj(uj)>,且其类型Vi为Uj&&;这可能在最近的gcc (我不知道)中得到了解决。
https://stackoverflow.com/questions/8746260
复制相似问题