所以我正试图想出一个函数来转换;
std::pair<T,std::pair<U, V>>
数据类型转换为std::tuple
;
std::tuple<T,U,V>
它应该在一般情况下工作,具有任意数量的混合类型参数,对的格式为;
- "汽车"永远是一种类型
- "cdr"将始终是CCD_ 2,
- 除了最里面的情况,其中"cdr"将是一个类型本身
(但是,这可能是std::pair
本身,因为类型是任意的)
- 除了最里面的情况,其中"cdr"将是一个类型本身
我想检索的参数的数量和类型是预先知道的,通过可变模板参数。
我目前的进度有点低,我一直在尝试一些事情,但我需要的代码似乎是这样的;
std::make_tuple(get<0>(pair), get<0>(get<1>(pair), get<0>(get<1>(get<1>(pair), ..., get<1>(pair)))));
然而,我似乎找不到自动生成它的方法,我尝试了Sequence<int...>
方法,但运气不好,但我确实认为这是我需要考虑的问题,例如,有一个get方法,它采用可变数量的索引,并使用这些索引多次查找,使用普通的get方法?
简单递归怎么样
#include <utility>
#include <tuple>
// 1. metafunction to concatenate a type and a tuple
template<typename T, typename U> struct tuple_prepend_type;
template<typename T, typename ...U>
struct tuple_prepend_type<T, std::tuple<U...>>
{
using type = std::tuple<T, U...>;
};
// 2. is_pair type trait
template<typename>
struct is_pair : std::false_type {};
template<typename U, typename V>
struct is_pair<std::pair<U, V>> : public std::true_type {};
// 3. the converter itself
template<typename T, typename = void>
struct pairs_to_tuple {
using type = std::tuple<typename T::first_type,
typename T::second_type>;
};
template<typename T>
struct pairs_to_tuple<T, typename std::enable_if<
is_pair<typename T::second_type>::value
>::type
>
{
using type = typename tuple_prepend_type<
typename T::first_type,
typename pairs_to_tuple<typename T::second_type>::type
>::type;
};
int main()
{
std::pair<int, std::pair<double, std::pair<bool, char> > > p;
static_assert(std::is_same<pairs_to_tuple<decltype(p)>::type,
std::tuple<int, double, bool, char>>::value, "")
}