Question

In my classes I use std::vector etc. as member variables, which come with their own move constructors. I don't explicitly declare move constructors for my classes and they are not implicitly declared for the most part.

If the implicit copy constructor or the implicit assignment operator of my class is called with an rvalue, are the members that have move constructors copied or moved?

If they are moved, is there any reason for trying to avoid temporaries when using classes with move constructible members?

Was it helpful?

Solution 2

Your copy constructor has only one signature whether implicitly created or explicitly declared, so there's no way for the compiler to generate different code depending on the right-hand operand of that constructor. It has to copy the movable attributes in all cases, because the copy constructor has only one set of code that must work for all possible inputs.

I think your last question may be performance-related but I can't tell for sure. What I would suggest is that you write the most obvious code and let the optimizer have a crack at it. Then profile the results and see if it shows that temporary creation and destruction are causing problems for you, and only if they are should you consider alternate mechanisms.

OTHER TIPS

If your class does not explicitly declare a move constructor/assignment operator and they are not declared implicitly either, then the implicitly declared copy ctor/assignment-op will not move the members, but copy them.

They would be moved if move ctor/assignment-op were declared implicitly for your class. You say they are not - why is that? Do your classes have custom destructors? You should strive for a design where you don't need a custom dtor, copy/move ctor and copy/move assignment op - sometimes called the "Rule of zero."

If you do need a custom dtor, you should declare move ctor/assignment-op explicitly to use move semantics. If your compiler supports it, you can declare them as defaulted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top