【发布时间】:2013-06-15 05:03:10
【问题描述】:
如何反转元组中的类型?例如,我希望 reverse_tuple<std::tuple<int, char, bool>>::type 成为 std::tuple<bool, char, int>。我尝试执行以下操作,但没有成功。我做错了什么?
#include <type_traits>
#include <tuple>
template <typename... Ts>
struct tuple_reverse;
template <typename T, typename... Ts>
struct tuple_reverse<std::tuple<T, Ts...>>
{
using type = typename tuple_reverse<
std::tuple<
typename tuple_reverse<std::tuple<Ts..., T>>::type
>
>::type;
};
template <typename T>
struct tuple_reverse<std::tuple<T>>
{
using type = std::tuple<T>;
};
int main()
{
using result_type = std::tuple<int, bool, char>;
static_assert(
std::is_same<
tuple_reverse<var>::type, std::tuple<char, bool, int>
>::value, ""
);
}
这是我的错误:
prog.cpp: In instantiation of ‘struct tuple_reverse<std::tuple<char, int, bool> >’:prog.cpp:15:34: recursively required from ‘struct tuple_reverse<std::tuple<bool, char, int> >’prog.cpp:15:34: required from ‘struct tuple_reverse<std::tuple<int, bool, char> >’prog.cpp:29:31: required from hereprog.cpp:15:34: error: no type named ‘type’ in ‘struct tuple_reverse<std::tuple<int, bool, char> >’prog.cpp: In function ‘int main()’:prog.cpp:30:9: error: template argument 1 is invalid
【问题讨论】:
-
我认为你不需要递归来做到这一点,tuple_cat,但你为什么要反转一个元组
标签: c++ c++11 tuples variadic-templates