I have a big class holding a lot of STL containers.
Will the compiler automatically make a move constructor that will move those containers to the target or I have to make my own?
Asked
Active
Viewed 1.4k times
41

R. Martinho Fernandes
- 228,013
- 71
- 433
- 510

Daniel
- 30,896
- 18
- 85
- 139
2 Answers
63
A move constructor for a class X is implicitly declared as defaulted exactly when
- X does not have a user-declared copy constructor,
- X does not have a user-declared copy assignment operator,
- X does not have a user-declared move assignment operator,
- X does not have a user-declared destructor, and
- the move constructor would not be implicitly defined as deleted.
So for example, if your class has a class type data member that does not have a move constructor, your class will not get a move constructor even if it doesn't have any copy/move constructor declared, because the implicitly declared move constructor would be defined as deleted (because of that data member).

Johannes Schaub - litb
- 496,577
- 130
- 894
- 1,212
-
6Interestingly, a user-declared move-constructor does not prevent the compiler from implicitly declaring a copy constructor. Maybe, that is worth mentioning here. At least this is what http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html says. – Tobias Apr 28 '14 at 07:32
-
8It's interesting that **all** members are required to have a move constructor. It seems sane to move all the members that can be moved and copy the ones that can't. – Kevin Cox Aug 12 '14 at 18:11
-
1So the best shot is to define move ctor for yourself, don't rely on compiler. – Deqing Sep 22 '16 at 02:34
-
And the said class member would not have an implicit move constructor because one or more of the items in the above list was not met. – wulfgarpro Mar 08 '17 at 23:18
-
1@KevinCox It is common to guarantee that a move constructor does not throw an exception, while a copy constructor often can (e.g. out of memory), so this might not be a good idea. You could argue "that's ok, the implicit move constructor just wouldn't be noexcept" but I still think there's potential for surprise (i.e. bugs). – Arthur Tacca Aug 25 '17 at 09:35
-
So, if I move a class that doesn't have automatically generated move constructor, then automatically generated copy constructor is used? :/ – anton_rh Feb 06 '18 at 13:39
-
1It's probably better to use explicitly defaulted move constructors: https://stackoverflow.com/questions/18290523/is-a-default-move-constructor-equivalent-to-a-member-wise-move-constructor – anton_rh Feb 06 '18 at 13:49
4
Default move constructors are generally tied to default copy constructors. You get one when you get the other. However, if you write a copy constructor/assignment operator, then no default copy and move constructors/assignment operators are written. If you write one of either set, you must write them all.

Nicol Bolas
- 449,505
- 63
- 781
- 982
-
1According to cppreference, an implicit copy constructor is provided even if you have an explicit move constructor by the user. – Shiladitya Mukherjee Dec 15 '22 at 17:43