[ACCEPTED]-Using move semantics with std::pair or std::tuple-move-constructor

Accepted answer
Score: 20

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

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

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

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

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

GCC 4.3.2 must not have a complete implementation. pair 2 (and tuple) shall have move constructors:

template<class U, class V> pair(U&& x, V&& y);
Effects: The constructor initializes first with std::forward(x) and second with std::forward(y).
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).

(From 1 [pairs.pair] in n3126)

More Related questions