Here指出C ++ 17中的演绎指南会使std::make_tuple
过时。但是,据我所知,std::make_tuple
与std::tuple::tuple
的标准扣除指南之间的差异在于std::reference_wrapper
,std::make_tuple
会推断出参考。
如何使用演绎指南实施此扣除?类似的东西,但扩展到Args...
具有的模板std::tuple::tuple
:
#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_)>);
}
答案 0 :(得分: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<T>::type...>;