Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:为什么没有调用move构造函数?_C++_Move Constructor - Fatal编程技术网

C++ C++;:为什么没有调用move构造函数?

C++ C++;:为什么没有调用move构造函数?,c++,move-constructor,C++,Move Constructor,我正在试验以下代码: #include <iostream> #include <utility> using namespace std; class A { int data; public: A(): data{0} { } A(const A& other) { print(other); } A(A&& other) { print(other)

我正在试验以下代码:

#include <iostream>
#include <utility>
using namespace std;

class A
{
    int data;
public:
   A(): data{0}
   {

   }

   A(const A& other)
   {
       print(other);
   }


   A(A&& other)
   {
       print(other);
   }

   void print(const A& other) const
   {
      cout << "In print 1" << endl;
   }

   void print(const A&& other) const
   {
      cout << "In print 2" << endl;
   }

};


int main() {
    A a0;
    A a1(a0);
    A a2(A());

    return 0;
}
然而,问题是:

显然,没有调用move构造函数。为什么会这样?在构建
a2
的过程中,什么被称为它的位置?

因为
a2(A())实际上是函数声明,而不是对象声明。见此:

如果要查看移动构造函数,请执行以下操作:

A a2((std::move(A())));
因为
a2(A())实际上是函数声明,而不是对象声明。见此:

如果要查看移动构造函数,请执行以下操作:

A a2((std::move(A())));

a3(std::move(a0))如果你在C++17中,那么你不会在
a2(A{})中观察到move-(或copy-)结构
a2=A()-所有这些都将使用
A
的默认构造函数初始化
a2
。A a3(std::move(a0))如果你在C++17中,那么你就不会在
A a2(A{})中观察到move-(或copy-)构造
a2=A()-所有这些都将使用
A
的默认构造函数初始化
a2
。没有创建、复制、移动或分配临时文件。或
a2(A{})(如果未省略移动构造函数)。请注意,这两种方法都不是
a2=A()
nor
a2(A{})
(或者类似于
aa2=A(A(A());
的东西调用C++17中的移动构造函数。临时变量永远不会被具体化。请参阅。或者
aa2(A{});
(如果没有省略移动构造函数)。请注意
aa2=A();
或者
aa2(A{});
(或者类似
aa2=A(A(A())的东西);
在C++17中调用移动构造函数。临时值永远不会具体化。请参阅。
A a2((std::move(A())));