0

See implementation of make_tuple in std library

      // NB: DR 705.
  template<typename... _Elements>
    constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
    make_tuple(_Elements&&... __args)
    {
      typedef tuple<typename __decay_and_strip<_Elements>::__type...>
    __result_type;
      return __result_type(std::forward<_Elements>(__args)...);
    }

The return type is decayed (reference removed)? so why it that ? what is the concern of this design ?

I know we have some alternative ways to achieve my requirements, e.g. boost::ref.

My question is why make_tuple has to remove the reference of types of input?

Update: Can anyone help on this discussion of my question ?

lsbbo
  • 304
  • 3
  • 12
  • possible duplicate of http://stackoverflow.com/questions/7867220/stdmake-tuple-doesnt-make-references – JustRufus Apr 03 '17 at 06:37
  • @JustRufus Thank you, but my question is why we have to remove the reference of the type inside make_tuple, I know there are some alternative ways to handle my case mentioned in that post, but that it not what I want to ask. – lsbbo Apr 03 '17 at 06:44

1 Answers1

0

I was curious and reading up on this as well and I found this - I believe this is what you're looking for (see section 1 in the linked paper): https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2069.html (Note: std::pair is a specific case of a std::tuple with two elements.)

TLDR: Arrays are not constructible with T() and not copy-constructible. Before decay was implemented in std::make_pair, it was not possible to have an efficient version of std::make_pair.

If anyone is wondering how should you build a tuple, consult this

In summary, when you need to build a tuple, use:

std::make_tuple if you need values in the returned tuple,

std::tie if you need lvalue references in the returned tuple,

std::forward_as_tuple if you need to keep the types of references of the inputs to build the tuple.

-- Fluent C++

taneres
  • 21
  • 4