C ++ rvalue引用了对左值引用的赋值

时间:2017-08-26 17:27:32

标签: c++ rvalue

让我们假设:

template<typename T>
T&& f(T&& t) {
  return std::forward<T>(t);
}

Presenter p;

auto x = f(std::move(p));   // x is new Presenter object - move ctor invoked
auto x = f(p);                // x is new Presenter object - copy ctor invoked
auto& x = f(p);             // x is lvalueRef, no copy / move ctor invoked
auto&& x = f(p);       // x is lvalueRef, no copy /move ctor invoked
auto&& x = f(std::move(p));   // x is rvalueRef, no copy / move ctor invoked

这很好,对于那种情况而言:

auto& x = f(std::move(p));  // x is lvalueRef, no copy / move ctor invoked

为什么我们能够协助lvalueRef rvalueRef?

例如我们不能这样做:

auto& c = 1;

所有帮助表示赞赏

0 个答案:

没有答案