为什么我不需要std :: move到std :: bind'ed函数?

And*_*ack 1 c++ g++ rvalue-reference stdbind c++11

假设我有一个函数,采用右值引用:

void whatever (std::unique_ptr<int>&&) {
    // Nothing!
}
Run Code Online (Sandbox Code Playgroud)

...并将其一个参数绑定到占位符.

auto f = std::bind(&whatever, _1);
Run Code Online (Sandbox Code Playgroud)

我尝试了这样的调用,结果与我的期望相反.

std::unique_ptr<int> nothing;
f(std::move(nothing));  // Fails to compile!
f(nothing);             // Works, but seems wrong!
Run Code Online (Sandbox Code Playgroud)

这是编译器错误吗?或者,工作调用是不安全的代码?或者为什么我没有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> >)'
Run Code Online (Sandbox Code Playgroud)

How*_*ant 5

使用libc ++时,我得到了相反的结果.

std::unique_ptr<int> nothing;
f(std::move(nothing));  // Works!
f(nothing);             // Fails to compile!
Run Code Online (Sandbox Code Playgroud)

我相信这是一个gcc4.4错误.[func.bind.bind]/p10/b3描述了这种情况:

  • 如果该值jis_placeholder<TiD>::value不为零时,所述参数是std::forward<Uj(uj)>与它的类型ViUj&&;

这可以在更新的gcc中修复(我不知道).