简体   繁体   中英

result_of, make_tuple, parameter pack

I would like to obtain the type returned by std::make_tuple for a given parameter pack. Up to now I have written the following code:

#include <tuple>
#include <functional>

template <class T>
struct unwrap_refwrapper
{
    using type = T;
};

template <class T>
struct unwrap_refwrapper<std::reference_wrapper<T>>
{
    using type = T&;
};

template <class T>
using special_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type;

template<class ... Types>
struct foo
{
    typedef std::tuple<special_decay_t<Types>...> tuple_t;
};

int main()
{
    short s;
    // t should be std::tuple<int, double&, short&>
    typedef foo<int, double&, decltype(std::ref(s))>::tuple_t t;
}

But I find it quite ugly to copy part of the possible implementation of std::make_tuple , which I did here.

I would like to achieve the given effect using std::result_of or something of that kind.

My attempt looks as follows:

#include <tuple>
#include <functional>

template<class ... Types>
struct foo
{
    typedef typename std::result_of<
        std::make_tuple(Types...)>::type tuple_t;
};

int main()
{
    short s;
    // t should be std::tuple<int, double&, short&>
    typedef foo<int, double&, decltype(std::ref(s))>::tuple_t t;
}

but it does not compile .

How can it be done?

template<class... Ts>
struct foo
{
    using tuple_t = decltype(std::make_tuple(std::declval<Ts>()...));
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM