【问题标题】:Is it possible to implement the whole std::make_tuple functionality via deduction guides?是否可以通过推导指南实现整个 std::make_tuple 功能?
【发布时间】:2018-06-01 02:28:57
【问题描述】:

Here 指出 C++17 中的推导指南将使std::make_tuple 过时。但是,据我了解,std::make_tuplestd::tuple::tuple 的标准推导指南之间的区别在于,给定std::reference_wrapperstd::make_tuple 将推导参考。

如何通过扣减指南实施扣减?类似的东西,但扩展到std::tuple::tuple 具有的模板Args...

#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.

【问题讨论】:

  • std::make_tuple 的原因是它在 C++17 中的演绎指南之前。
  • @SamVarshavchik,抱歉不完整,我更新了标题中的问题。
  • 这个问题好像是primarily opinion-based
  • @xskxzr,用更具体的东西改写了标题。

标签: c++ c++17 stdtuple


【解决方案1】:
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...>;

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 2023-04-04
    • 2019-02-02
    • 2018-02-10
    • 1970-01-01
    • 2020-03-22
    • 2020-03-24
    • 1970-01-01
    • 2014-10-14
    相关资源
    最近更新 更多