In Exceptional C++, Herb Sutter wrote about auto_ptr:
The problem is that auto_ptr does not quite meet the requirements of a
type you can put into containers, because copies of auto_ptrs are not
equivalent.
The book was written with respect to C++03, and I wonder whether this is still valid as this code seems to compile perfectly under GCC 4.7.1:
#include <vector>
struct Foo
{
Foo() { }
Foo( Foo&& ) { }
Foo( Foo& ) = delete;
Foo& operator= (Foo&&) { return *this; }
Foo& operator= (Foo&) = delete;
};
int main()
{
std::vector<Foo> bar;
bar.push_back(Foo());
}
But accepting moveable-but-not-copyable objects could also be a GCC extension. I am not sure. What are the requirements for std::vector objects?
To be able to use
std::vector<Foo>::push_back(), the typeFooneeds to beMoveInsertableorCopyInsertable(23.2.1 [container.requirements.general] paragraph 13) according to 23.2.3 [sequence.rqmts] paragraph 16 (Table 101). Clearly, providing theMoveInsertablemodel requires that you usepush_back()with an r-value, possibly obtained usingstd::move(x). That is, you don’t even need the move assignment operator to be able to usestd::vector<...>::push_back(). I think not all standard libraries correctly take this latter part into account and delegate toinsert(): delegating toinsert()doesn’t work according to the C++ 2011 standard becauseinsert()also requires the type to beMoveAssignableorCopyAssignable.Trying to search for
push_backunfortunately fails to point to Table 101 for me because the name is split intopush_andback()by a line break…