xmllmx
xmllmx

Reputation: 42371

Why does `std::pair` take `std::tuple` as ctor argument type rather than `const std::tuple&`?

The C++11 standard defines a constructor of std::pair as follows:

template<class... Args1, class... Args2>
pair(std::piecewise_construct_t, std::tuple<Args1...> first_args,
     std::tuple<Args2...> second_args);

Why does std::pair take std::tuple as ctor argument type rather than const std::tuple& ?

What if it is heavy to copy first_args and second_args?

Upvotes: 1

Views: 117

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477100

The intention is for those tuples to be tuples of references, as created by std::forward_as_tuple:

Foo x;
Bar y(1, 2, true);

std::pair<A, B> p(std::piecewise_construct,
                  std::forward_as_tuple(10, x, make()),
                  std::forward_as_tuple(std::move(y), false, get(), 'a'));

This constructs pair elements as if by A(10, x, make()) etc.

The actual tuple types are std::tuple<int &&, Foo &, Z &&> etc., which are light-weight.

Upvotes: 6

Related Questions