Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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+;中未调用移动构造函数+;0x_C++_C++11 - Fatal编程技术网

C++ 在C+;中未调用移动构造函数+;0x

C++ 在C+;中未调用移动构造函数+;0x,c++,c++11,C++,C++11,请在下面找到我的代码,我曾经调用move构造函数(源于其他站点的代码),并让我知道它有什么问题,我使用的是GCC4.5.3 #include <iostream> #include <vector> class Int_Smart_Pointer { int *m_p; public: Int_Smart_Pointer() { std::cout<<"Derfault Constructor"<< std::endl;

请在下面找到我的代码,我曾经调用move构造函数(源于其他站点的代码),并让我知道它有什么问题,我使用的是GCC4.5.3

#include <iostream>
#include <vector>

class Int_Smart_Pointer {

  int *m_p;

public:
  Int_Smart_Pointer() {
    std::cout<<"Derfault Constructor"<< std::endl;
    m_p = NULL;
  }

  explicit Int_Smart_Pointer(int n) {
    std::cout<<"Explicit Constructor: " << n <<std::endl;
    m_p = new int(n);
  }

  Int_Smart_Pointer(const Int_Smart_Pointer& other) {
    std::cout<<"Copy Constructor: "<<std::endl;
    if(other.m_p)
      m_p = new int(*other.m_p);
    else
      m_p = NULL;
  }

  Int_Smart_Pointer(Int_Smart_Pointer&& other) {
    std::cout<<"Move Constructor: "<<std::endl;
    m_p = other.m_p;
    other.m_p = NULL;
  }

  Int_Smart_Pointer& operator=(const Int_Smart_Pointer& other) {
    std::cout<<"Copy Assignment"<< std::endl;
    if(this != &other) {
         delete m_p;
         if(other.m_p)
           m_p = new int(*other.m_p);
         else
           m_p = NULL;
    }

      return *this;
  }

  Int_Smart_Pointer& operator=(Int_Smart_Pointer&& other) {
    std::cout<<"Move Assignment"<< std::endl;
    if(this != &other) {
      delete m_p;
      m_p = other.m_p;
      other.m_p = NULL;
    }

      return *this;
  }

  ~Int_Smart_Pointer() {
    std::cout<<"Default Destructor"<< std::endl;
    delete m_p;
  }

  int get() const{
    if(m_p)
      return *m_p;
    else
      return 0;
  }

};

Int_Smart_Pointer square(const Int_Smart_Pointer& r) {
    const int i = r.get();
    return Int_Smart_Pointer(i * i);
}

Int_Smart_Pointer getMove_Constructor() {
  return Int_Smart_Pointer(100);
}


int main()
{
  Int_Smart_Pointer a(10);
  Int_Smart_Pointer b(a);
  b = square(a);

  std::cout<< std::endl;

  Int_Smart_Pointer c(30);
  Int_Smart_Pointer d(square(c)); //Move constructor Should be called (rvalue)

  std::cout<< std::endl;

  Int_Smart_Pointer e(getMove_Constructor()); //Move constructor Should be called (rvalue)


  std::cout<< std::endl;

  std::vector<Int_Smart_Pointer> ptr_vec;
  ptr_vec.push_back(getMove_Constructor());
  ptr_vec.push_back(getMove_Constructor());

  std::cout<< std::endl;

  return 0;
}
当我们在构造时使用std::move时,它调用的是move构造函数

Int_Smart_Pointer d(std::move(square(c))); //used std::move and Move constructor called
Int_Smart_Pointer e(std::move(getMove_Constructor())); //used std::move works as above
但即使我们不使用,gerMove_构造函数和平方返回值在构造对象时变为右值,因为我们找不到地址空间或对它们的引用

请让我知道,如果我的理解有问题,如果没有,那么为什么不调用move构造函数

提前谢谢。
Satya

显然,必须在
square
getMove\u构造函数中调用取
int
的构造函数。
显然,在
Int\u Smart\u指针d(square(c))
Int\u Smart\u指针e(getMove\u Constructor())
中,没有调用其他构造函数,因为它们可以被编译器优化掉。

当按值返回局部变量时,编译器仍然可以像在c++03(返回值优化)中那样省略复制构造函数谢谢henrik,智能指针(Int&&n){std::cout
Int_Smart_Pointer d(std::move(square(c))); //used std::move and Move constructor called
Int_Smart_Pointer e(std::move(getMove_Constructor())); //used std::move works as above