1

Here it is pointed out that deduction guides in C++17 will make std::make_tuple obsolete. However, as I understand, the difference between std::make_tuple and the standard deduction guides for std::tuple::tuple is that given a std::reference_wrapper, std::make_tuple will deduce a reference.

How can this deduction be implemented with deduction guides? Something like that, but extended to template Args... that std::tuple::tuple has:

#include <tuple>
#include <functional>

template <typename T>
struct Element {
    Element(std::reference_wrapper<std::decay_t<T>> rw) : value_{rw.get()} {}
    Element(T t) : value_{std::move(t)} {}

    T value_;
};

template <typename T> Element(T) -> Element<T>;
template <typename T> Element(std::reference_wrapper<T>) -> Element<T&>;
template <typename T> Element(std::reference_wrapper<const T>) -> Element<const T&>;

struct A {    
    int i;
};

int main()
{
    A a{10};

    Element wa{std::ref(a)};
    static_assert(std::is_lvalue_reference_v<decltype(wa.value_)>);

    Element wb{A{15}};
    static_assert(std::is_object_v<decltype(wb.value_)>);
}

Example.

Dev Null
  • 4,731
  • 1
  • 30
  • 46

1 Answers1

4
template<class T> struct unwrap { using type = T; };
template<class T> struct unwrap<reference_wrapper<T>> { using type = T&; };

template<class... Ts>
tuple(Ts...) -> tuple<typename unwrap<Ts>::type...>;
Burak
  • 2,251
  • 1
  • 16
  • 33
T.C.
  • 133,968
  • 17
  • 288
  • 421