Consequences of the new Rule of 5 for C++ 11

B

Bob Langelaan

I hope that this topic has not already been posted on and my search skills were just too poor to find it.

My understanding of the old Rule of 3 was that if your class required one of:

1) destructor
2) overloaded copy assignment operator
3) programmer supplied copy constructor

then you class would likely require all 3 of the above. A specific examplewould be where the class ctor(s) dynamically allocates storage which is then assigned to a pointer which is a data member of the class.

From what I understand, the new move semantics added to the C++ language with advent of C++ 11 add 2 new methods to the rule of 3 so that it now becomes the rule of 5. The 2 new methods are:

4) overloaded move assignment operator
5) programmer supplied move constructor

Does this mean that if a programmer fails to add a overloaded move assignment operator and a move ctor to an exisitng class when moving form a pre C++11 compiler to a C++ 11 compiler that their solution is broken? Won't the compiler in such a situation automatically create a move ctor and a move assignment operator which will not correctly handle the moving of the dynamicresource?

Thanks in advance,

Bob
 
V

Victor Bazarov

I hope that this topic has not already been posted on and my search
skills were just too poor to find it.
My understanding of the old Rule of 3 was that if your class
required one of:

1) destructor
2) overloaded copy assignment operator
3) programmer supplied copy constructor

then you class would likely require all 3 of the above. A specific
example would be where the class ctor(s) dynamically allocates storage
which is then assigned to a pointer which is a data member of the class.
From what I understand, the new move semantics added to the C++
language with advent of C++ 11 add 2 new methods to the rule of 3 so
that it now becomes the rule of 5. The 2 new methods are:
4) overloaded move assignment operator
5) programmer supplied move constructor

From the Standard, [class.copy/9]

<<If the definition of a class X does not explicitly declare a move
constructor, one will be implicitly declared
as defaulted if and only if
— 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, if you define a copy c-tor, no move c-tor is provided. And you only
need to provide one when you want to override the behavior (member-wise
copying) provided by the compiler.

Same with the assignment op, I'm sure.
Does this mean that if a programmer fails to add a overloaded move
assignment operator and a move ctor to an exisitng class when moving
form a pre C++ 11 compiler to a C++ 11 compiler that their solution is
broken? Won't the compiler in such a situation automatically create a
move ctor and a move assignment operator which will not correctly handle
the moving of the dynamic resource?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top