lundi 31 août 2015

How to get the move constructor calling

Consider following code:

class Base {
public:
    int bi;
    Base() : bi(100)    {std::cout << "\nBase default constructor ...";}
    Base(int i) : bi(i) {std::cout << "\nBase int constructor: "<< bi;}
    Base(const Base& b) {std::cout << "\nBase copy constructor";}
    Base(Base&& b)      {std::cout << "\nBase move constructor";}
};

Base getBase() {
    cout << "\nIn getBase()";
    return Base();  
}
int main() {
    Base b2(getBase());  
    Base b3 = Base(2);   
    Base b4 = getBase(); 
}

In spite of rvalues being given, none of the above constructions in main are calling the move constructor. Is there a way to ensure that user defined move constructor is called?

Here is what I am getting:

In getBase()    
Base default constructor ...
Base int constructor: 2
In getBase()
Base default constructor ...
Base destructor: 100
Base destructor: 2
Base destructor: 100

Aucun commentaire:

Enregistrer un commentaire