小编use*_*549的帖子

为什么在我的代码中调用复制构造函数而不是移动构造函数?

我试图了解移动构造函数和右值引用。所以我在https://www.onlinegdb.com/online_c++_compiler上尝试了此代码。但是结果使我感到困惑。

#include <iostream>
#include <type_traits>


class A {
public:
  A() { std::cout << "Constructed" << std::endl; }
  A(const A& )= delete;
  A(A&&) { std::cout << "Move Constructed" << std::endl; }
};

int
main ()
{
  A&& a = A();
  A b = a; // error: use of deleted function ‘A::A(const A&)’
  //A b = static_cast<decltype(a)>(a); // This works, WTF?
  std::cout << std::is_rvalue_reference<decltype(a)>::value << std::endl; // pretty sure a is rvalue reference.

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

c++ rvalue-reference c++11

2
推荐指数
1
解决办法
67
查看次数

标签 统计

c++ ×1

c++11 ×1

rvalue-reference ×1