dimanche 8 septembre 2019

How does C++ handle double moves?

For example, if I have a function:

struct my_type {
   void add(some_type st) {
       values_.emplace_back(
           std::move(st)
       );
   }
   vector<some_type> values_;
};

And I call the member:

int main() {
    my_type mt;
    some_type st;
    mt.add(std::move(st));
}

I've moved st into the argument for add, which them just turns right around and moves it into a container it's managing.

This turns up quite a lot where I've got wrapper around some underlying container.

Does the C++ language allow/require some optimization of this "double move" pattern? Or should I be writing separate lvalue and rvalue add functions?

Aucun commentaire:

Enregistrer un commentaire