I am trying some new C++11 features on visual studio 11, started with the move constructor. I wrote a simple class called "MyClass" containing a move constructor:
class MyClass
{
public:
explicit MyClass( int aiCount )
: mpiSize( new int( aiCount ) ),
miSize2( aiCount)
{
}
MyClass( MyClass&& rcOther )
: mpiSize( rcOther.mpiSize )
, miSize( *rcOther.mpiSize )
{
rcOther.mpiSize = 0;
rcOther.miSize = 0;
}
~MyClass()
{
delete mpiSize;
}
private:
int *mpiSize;
int miSize2;
};
I got there questions here:
- I assumed that the compiler would generate a move constructor for MyClass if I don't implement one - but it doesn't seems so?
- Is the implementation of the move constructor correct for MyClass?
- Is there a better way to implement the move constructor for MyClass?
MSVC++ implemented move constructors before the final version of the standard was out. In the version of the standard MSVC++'s implementation was based on, the rules for generating a default move constructor were ridiculously more strict than they are in the final version of the standard. See here: Why is this code trying to call the copy constructor? (specifically this answer and the comments on it) for more info on that. This has not been and will not be fixed in Visual Studio 11,
for some unknown stupid reasonbecause they had other priorities.No, you need to call
std::move
on the members ofrcOther
, and you initialise members with the corresponding members from the dying object (you misnamedmiSize
):It doesn't make a difference for built in types like
int
andint*
, but it definitely makes a difference for user-defined types.std::move
just returns the argument casted into aT&&
, an rvalue-reference, so that the correct constructor (the move constructor,T(T&&)
) is called for each of the sub-objects. If you don't usestd::move
on the members of the dying object, they will be treated likeT&
, and the copy constructor of your subobjects (T(T&)
) will be called instead of the move constructor. That is very bad and thwarts almost the entire purpose of you having written a move constructor.3. You are doing some unnecessary things, like setting the integer to 0. You only need to set the pointer to 0 so that
delete
ing it won't delete the resource of the new object you created.Also, if this is not a didactic exercise, you may want to consider using
std::unique_ptr
instead of managing the lifetime of your own object. This way you don't even have to write a destructor for your class. Note that if you do that, usingstd::move
to initialise the member from the dying member in the move constructor would be mandatory.FiveThree, Four, or Five. See his answer for more details on this.Why doesn’t the compiler auto-generate a move constructor?
The compiler does generate a move constructor if you don’t do so – after a fashion. However, the compiler cannot second-guess your motives so it doesn’t know what the pointer in your class does. In particular, it doesn’t know that the pointer confers ownership of memory and needs to be nulled out.
Is the implementation of the move constructor correct?
The move constructor is correct1 but the rest of the class isn’t, you are violating the rule of three: your class needs an appropriate copy constructor and copy assignment operator.Is there a better way to implement the move constructor?
A better way to write the move constructor looks as follows:
Two comments:
rcOther.mpiSize
? While this isn’t wrong, it also makes no sense and is misleading.But an even better way is to rely on pre-existing facilities. In this case, you want to model memory ownership. A naked pointer does this poorly, you should use a
std::unique_ptr
instead. This way, you don’t need to implement either destructor nor move constructor since the the auto-generated methods do the right thing.1 Caveat: See Seth’s answer for a better explanation that mentions
std::move
(which is a no-op in this particular case, however).