void push_back(value_type&& _Val)
{ // insert by moving into element at end
if (_Inside(_STD addressof(_Val)))
{ // push back an element
size_type _Idx = _STD addressof(_Val) - this->_Myfirst;
if (this->_Mylast == this->_Myend)
_Reserve(1);
_Orphan_range(this->_Mylast, this->_Mylast);
this->_Getal().construct(this->_Mylast,
_STD forward<value_type>(this->_Myfirst[_Idx]));
++this->_Mylast;
}
else
{ // push back a non-element
if (this->_Mylast == this->_Myend)
_Reserve(1);
_Orphan_range(this->_Mylast, this->_Mylast);
this->_Getal().construct(this->_Mylast,
_STD forward<value_type>(_Val));
++this->_Mylast;
}
}
I revised your code and this shows you that the objects are destructed properly.
In the process you leaked 3 CBox objects whose destructors are not called. In a sense, your code does the opposite of what you claim. [edit: Actually I didn't look closely enough at your code in the output tags -- the code is fine (would be better with std::unique_ptr), but it doesn't illustrate anything.]
@OP:
When you push_back(CBox(1,2,3)) you create a temporary CBox object which is copied into the vector. The temporary's constructor is invoked immediately after push_back returns.
I wouldn't expect that of emplace back. A little experimentation leads me to believe emplace_back is not implemented correctly for contained objects that are copy- but not move-constructable. GCC doesn't display the same behavior.
If you define a move constructor for your class I think you'll see the expected behavior, although it shouldn't be necessary.
[Edit: Doesn't seem to be a bug report on Microsoft Connect - I'll submit one when I have more time if nobody beats me to it.]
Wait but how would the move constructor really work? There isn't much effort in constructing or deconstructing the CBox so I am a bit confused. You cant really "move" a CBox?
I know about the 3 first calls, but what about the 3 other calls that make it 6?
In the push_back code there should be 6. The three temporaries that are destroyed when their respective push_backs return, and the three stored in the vector that are destroyed when the vector goes out of scope. Using emplace_back, there should be 3.
Wait but how would the move constructor really work? There isn't much effort in constructing or deconstructing the CBox so I am a bit confused. You cant really "move" a CBox?
In this case the move constructor would look exactly like the copy constructor with the obvious exception of the parameter type. As I said, it shouldn't be required to get the expected behavior with emplace_back, but apparently with VC++ it is.