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_)>);
}