std :: bind和rvalue引用

Gil*_*esz 8 c++ rvalue-reference c++11

让我们考虑以下代码:

class Widget{
};

int main(){
Widget w;
auto lambda = bind([](Widget&& ref){ return; }, std::move(w));

return 0;
}
Run Code Online (Sandbox Code Playgroud)

它会触发错误

no match for call to ‘(std::_Bind<main()::<lambda(Widget&&)>(Widget)>) ()’
     lambda();
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么错误出现了?毕竟,我对rvalue引用进行了显式转换 - 我的意思是std::move(w)我通过rvalue引用参数 - 我的意思是Widget&& ref.

这是怎么回事?

而且下面的代码有效,让我更担心的是:

class Widget{
};

int main(){
Widget w;
auto lambda = bind([](Widget& ref){ return; }, std::move(w));

return 0;
}
Run Code Online (Sandbox Code Playgroud)

5go*_*der 11

如果你写下std::bind示意图的内容,可能会更清楚.

// C++14, you'll have to write a lot of boilerplate code for C++11
template <typename FuncT, typename ArgT>
auto
bind(FuncT&& func, ArgT&& arg)
{
  return
    [
      f = std::forward<FuncT>(func),
      a = std::forward<ArgT>(arg)
    ]() mutable { return f(a); };  // NB: a is an lvalue here
}
Run Code Online (Sandbox Code Playgroud)

由于您可以std::bind多次调用函数对象,因此它不能"使用"捕获的参数,因此它将作为左值引用传递.您自己传递bindrvalue 这一事实意味着在a初始化的行上没有复制.

如果您尝试使用bind上面显示的原理图编译示例,您还将从编译器中获得更有用的错误消息.

main.cxx: In instantiation of ‘bind(FuncT&&, ArgT&&)::<lambda()> mutable [with FuncT = main()::<lambda(Widget&&)>; ArgT = Widget]’:
main.cxx:10:33:   required from ‘struct bind(FuncT&&, ArgT&&) [with FuncT = main()::<lambda(Widget&&)>; ArgT = Widget]::<lambda()>’
main.cxx:11:31:   required from ‘auto bind(FuncT&&, ArgT&&) [with FuncT = main()::<lambda(Widget&&)>; ArgT = Widget]’
main.cxx:18:59:   required from here
main.cxx:11:26: error: no match for call to ‘(main()::<lambda(Widget&&)>) (Widget&)’
    ]() mutable { return f(a); };  // NB: a is an lvalue here
                          ^
main.cxx:11:26: note: candidate: void (*)(Widget&&) <conversion>
main.cxx:11:26: note:   conversion of argument 2 would be ill-formed:
main.cxx:11:26: error: cannot bind ‘Widget’ lvalue to ‘Widget&&’
main.cxx:18:33: note: candidate: main()::<lambda(Widget&&)> <near match>
   auto lambda = bind([](Widget&&){ return; }, std::move(w));
                                 ^
main.cxx:18:33: note:   conversion of argument 1 would be ill-formed:
main.cxx:11:26: error: cannot bind ‘Widget’ lvalue to ‘Widget&&’
    ]() mutable { return f(a); };  // NB: a is an lvalue here
Run Code Online (Sandbox Code Playgroud)