C++ 在没有右值引用的情况下移动构造函数?

C++ move constructors without rvalue references?

我刚刚在 Boost 中找到了一些代码,解释如下:

class A {};

class B {
public:
  B(A a): a_(std::move(a)) {}
private:
  A a_;
};

int main() {
  A a;
  B b(std::move(a));
}

假设这行得通,为什么要使用 move constructor/assignment 和右值引用?还是移动 constructor/assignment 就可以省去第二次 std::move() 调用的麻烦?

C++ move constructors without rvalue references?

B(A) 不是移动构造函数。它是一个转换构造函数。

Assuming this works

这个转换构造函数确实有效。

why use move constructor/assignment with an rvalue reference?

因为没有右值引用就无法移动constructor/assignment。

如果您想问,为什么使用右值引用转换构造函数:它可以避免一次移动构造。这样做的缺点是需要对左值引用进行额外的重载,以防您希望能够从这些引用中进行复制。

我的 "paraphrased" 代码实际上来自 this 问题引用的代码,所以我的问题已经被提出并回答了。