mardi 21 juin 2022

Using std::move when returning an object [duplicate]

If I have the following function:

MyObject Process(std::string par1, int par2){
     MyObject message;
     // Do some processing here
    return message;
}

Can this be implemented like this:

MyObject Process(std::string par1, int par2){
         MyObject message;
         // Do some processing here
        return std::move(message);
    }

And what is the difference between both? (I have the notion that the second one will not copy the object message but just move it, but since message function scope is finishing, what does this represent?

Aucun commentaire:

Enregistrer un commentaire