Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move constructor is not getting called in C++0x

Tags:

c++

c++11

Please find my code below, I have used to call move constructor (code Inspired from other sites)and let me know whats wrong with it, I am using GCC 4.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;
}

And the output is

Explicit Constructor: 10
Copy Constructor:
Explicit Constructor: 100
Move Assignment
Default Destructor

Explicit Constructor: 30
Explicit Constructor: 900

Explicit Constructor: 100

Explicit Constructor: 100
Move Constructor:
Default Destructor
Explicit Constructor: 100
Move Constructor:
Move Constructor:
Default Destructor
Default Destructor

Default Destructor
Default Destructor
Default Destructor
Default Destructor
Default Destructor
Default Destructor
Default Destructor

When we use std::move while constructing, it is calling move constructor.

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

But even if we do not use, gerMove_Constructor and square return values becomes rvalue while constructing object as we can not find address space or reference to them,

Please let me know if something is wrong in my understanding, if not then why move constructor is not called.

Thanks In Advance. Satya

like image 464
Satya Ravipati Avatar asked Oct 04 '11 09:10

Satya Ravipati


People also ask

Are move constructors automatically generated?

If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.

What does std :: move () do?

std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular, std::move produces an xvalue expression that identifies its argument t . It is exactly equivalent to a static_cast to an rvalue reference type.

What does move () do in C++?

Move Constructor And Semantics: std::move() is a function used to convert an lvalue reference into the rvalue reference. Used to move the resources from a source object i.e. for efficient transfer of resources from one object to another.


1 Answers

When returning a local variable by value, the compiler is still allowed to elide copy constructors as in C++03 (return value optimization).

like image 91
visitor Avatar answered Oct 06 '22 13:10

visitor