Using move semantics with std::pair or std::tuple

Channel72 picture Channel72 · Nov 4, 2010 · Viewed 13.8k times · Source

Suppose you want to take advantage of move semantics, but one of your movable classes needs to be part of an std::pair. The purpose would be to create a function that returns an std::pair that can be treated as an rvalue, and forwarded along.

But I can't see how this can be done, unless an internal change to std::pair itself is made, to make it aware of move semantics.

Consider the following code:

struct Foo
{
 Foo() { }

 Foo(Foo&& f) { }

 private:

 Foo(const Foo& f) { } // do not allow copying
};

int main() 
{
 Foo f;
 std::pair<Foo, int> res = std::make_pair(f, 10); // fails due to private copy constructor
}

The problem is that std::make_pair, as well as the std::pair constructor itself, takes two objects and tries to make internal copies of them. This causes it to try and invoke the copy constructor. But in my example, I want to be able to move the new pair into res, and ensure that no copies are made. I would think this wouldn't be possible unless std::pair itself had the following constructor defined internally:

pair(T1&& t1, T2&& t2) : first(std::move(t1)), second(std::move(t2))

But it doesn't, at least not on the compiler I'm using (gcc 4.3.2). It may be that my compiler is simply out-of-date, and newer versions in fact will have this move-aware constructor. But my understanding of move semantics is somewhat flaky at the moment, so I'm not sure if I'm simply overlooking something here. So, is what I'm trying to accomplish possible, without actually reimplementing std::pair? Or is my compiler just out of date?

Answer

James McNellis picture James McNellis · Nov 4, 2010

That's not the std::pair constructor that will be called, though. The std::pair move constructor will be called, and the move constructor should do exactly what you expect (N3126 20.3.5.2/6):

template<class U, class V> pair(pair<U, V>&& p);

Effects: The constructor initializes first with std::move(p.first) and second with std::move(p.second).

However, your example should fail because in std::make_pair(f, 10);, f is an lvalue and needs to be explicitly moved, otherwise it is copied. The following should work:

std::pair<Foo, int> res = std::make_pair(std::move(f), 10);