如何反转元组中的类型?例如,我希望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 here
prog.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
答案 0 :(得分:18)
你做错了什么在这里:
using type = typename tuple_reverse<
std::tuple<
typename tuple_reverse<std::tuple<Ts..., T>>::type
>
>::type;
从内到外看,你重新排序元组元素:tuple<Ts..., T>
,然后你试图反转它,然后你把结果放在tuple
,然后你试图反转那 ......嗯?! :)
这意味着每次实例化tuple_reverse
时,都会给它一个相同大小的元组,因此它永远不会完成,并且会永远地递归实例化它自己。 (然后,如果该递归甚至完成,你将结果元组类型放入一个元组中,所以你有一个包含N元素元组的单元素元组,并反转它,这没有任何作用,因为反转单元素元组是一个无操作)。
你想剥离其中一个元素,然后反转剩下的元素,然后再将它连接起来:
using head = std::tuple<T>;
using tail = typename tuple_reverse<std::tuple<Ts...>>::type;
using type = decltype(std::tuple_cat(std::declval<tail>(), std::declval<head>()));
你不需要将它包装在一个元组中并再次反转它。)
你还应该处理空元组的情况,所以整个事情是:
template <typename... Ts>
struct tuple_reverse;
template <>
struct tuple_reverse<std::tuple<>>
{
using type = std::tuple<>;
};
template <typename T, typename... Ts>
struct tuple_reverse<std::tuple<T, Ts...>>
{
using head = std::tuple<T>;
using tail = typename tuple_reverse<std::tuple<Ts...>>::type;
using type = decltype(std::tuple_cat(std::declval<tail>(), std::declval<head>()));
};
我会这样做。
使用C ++ 14
获取类型template<typename T, size_t... I>
struct tuple_reverse_impl<T, std::index_sequence<I...>>
{
typedef std::tuple<typename std::tuple_element<sizeof...(I) - 1 - I, T>::type...> type;
};
// partial specialization for handling empty tuples:
template<typename T>
struct tuple_reverse_impl<T, std::index_sequence<>>
{
typedef T type;
};
template<typename T>
struct tuple_reverse<T>
: tuple_reverse_impl<T, std::make_index_sequence<std::tuple_size<T>::value>>
{ };
或者您可以编写一个函数来反转实际的元组对象,然后使用decltype(reverse(t))
来获取类型。在C ++ 14中反转类似元组的对象:
template<typename T, size_t... I>
auto
reverse_impl(T&& t, std::index_sequence<I...>)
{
return std::make_tuple(std::get<sizeof...(I) - 1 - I>(std::forward<T>(t))...);
}
template<typename T>
auto
reverse(T&& t)
{
return reverse_impl(std::forward<T>(t),
std::make_index_sequence<std::tuple_size<T>::value>());
}
在C ++ 11中使用<integer_seq.h>
并添加返回类型并使用remove_reference
从元组类型中删除引用(因为tuple_size
和tuple_element
不能使用对元组的引用):
template<typename T, typename TT = typename std::remove_reference<T>::type, size_t... I>
auto
reverse_impl(T&& t, redi::index_sequence<I...>)
-> std::tuple<typename std::tuple_element<sizeof...(I) - 1 - I, TT>::type...>
{
return std::make_tuple(std::get<sizeof...(I) - 1 - I>(std::forward<T>(t))...);
}
template<typename T, typename TT = typename std::remove_reference<T>::type>
auto
reverse(T&& t)
-> decltype(reverse_impl(std::forward<T>(t),
redi::make_index_sequence<std::tuple_size<TT>::value>()))
{
return reverse_impl(std::forward<T>(t),
redi::make_index_sequence<std::tuple_size<TT>::value>());
}
答案 1 :(得分:8)
未测试。
template < typename Tuple, typename T >
struct tuple_push;
template < typename T, typename ... Args >
struct tuple_push<std::tuple<Args...>, T>
{
typedef std::tuple<Args...,T> type;
};
template < typename Tuple >
struct tuple_reverse;
template < typename T, typename ... Args >
struct tuple_reverse<std::tuple<T, Args...>>
{
typedef typename tuple_push<typename tuple_reverse<std::tuple<Args...>>::type, T>::type type;
};
template < >
struct tuple_reverse<std::tuple<>>
{
typedef std::tuple<> type;
};
无论如何,还有什么东西。
这也只会扭转类型,这似乎就是你所追求的。反转实际元组将涉及函数,而不是元函数。
答案 2 :(得分:2)
我在处理任意类型的反转模板参数时遇到了这个问题。
Jonathan Wakely的回答对元组很有用,但万一其他人需要反转任何类型,即T<P1, P2, ..., Pn>
到T<Pn, Pn-1, ..., P1>
,这就是我提出的问题({{3 }})。
namespace Details
{
/// Get the base case template type `T<>` of a templated type `T<...>`
template<typename>
struct templated_base_case;
template <template<typename...> class T, typename... TArgs>
struct templated_base_case<T<TArgs...>>
{
using type = T<>;
};
/// Inner reverse logic.
///
/// Reverses the template parameters of a templated type `T` such
/// that `T<A, B, C>` becomes `T<C, B, A>`.
///
/// Note that this requires `T<>` to exist.
template<
typename T,
typename = typename templated_base_case<T>::type>
struct reverse_impl;
template<
template <typename...> class T,
typename... TArgs>
struct reverse_impl<
typename templated_base_case<T<TArgs...>>::type,
T<TArgs...>>
{
using type = T<TArgs...>;
};
template<
template<typename...> class T,
typename first,
typename... rest,
typename... done>
struct reverse_impl<
T<first, rest...>,
T<done...>>
{
using type = typename reverse_impl <T<rest...>, T<first, done...>>::type;
};
/// Swap template parameters of two templated types.
///
/// `L<A, B, C> and R<X, Y, Z>` become `L<X, Y, Z> and R<A, B, C>`.
template<typename L, typename R>
struct swap_template_parameters;
template<
template<typename...> class L,
template<typename...> class R,
typename... x,
typename... y>
struct swap_template_parameters<L<x...>, R<y...>>
{
using left_type = L<y...>;
using right_type = R<x...>;
};
}
/// Parameter pack list of types
template <typename... Args>
struct type_list { };
/// Reverses the arguments of a templates type `T`.
///
/// This uses a `type_list` to allow reversing types like std::pair
/// where `std::pair<>` and `std::pair<T>` are not valid.
template<typename T>
struct reverse_type;
template<template<typename...> class T, typename... TArgs>
struct reverse_type<T<TArgs...>>
{
using type = typename Details::swap_template_parameters<
T<TArgs...>,
typename Details::reverse_impl<type_list<TArgs...>>::type>::left_type;
};
可以组合一些实现逻辑,但我试图在这里尽可能清楚。
reverse_type
可以应用于元组:
using my_tuple = std::tuple<int, bool, char>;
static_assert(
std::is_same<
typename reverse_type<my_typle>::type,
std::tuple<char, bool, int>>::value,
"");
或其他类型:
/// Standard collections cannot be directly reversed easily
/// because they take default template parameters such as Allocator.
template<typename K, typename V>
struct simple_map : std::unordered_map<K, V> { };
static_assert(
std::is_same<
typename reverse_type<simple_map<std::string, int>>::type,
simple_map<int, std::string>>::value,
"");
答案 3 :(得分:0)
出于兴趣,您是否真的想要反转元组类型,或者只是以相反的顺序处理每个元素(在我的项目中更常见)?
#include <utility>
#include <tuple>
#include <iostream>
namespace detail {
template<class F, class Tuple, std::size_t...Is>
auto invoke_over_tuple(F &&f, Tuple &&tuple, std::index_sequence<Is...>) {
using expand = int[];
void(expand{0,
((f(std::get<Is>(std::forward<Tuple>(tuple)))), 0)...});
}
template<class Sequence, std::size_t I>
struct append;
template<std::size_t I, std::size_t...Is>
struct append<std::index_sequence<Is...>, I> {
using result = std::index_sequence<Is..., I>;
};
template<class Sequence>
struct reverse;
template<>
struct reverse<std::index_sequence<>> {
using type = std::index_sequence<>;
};
template<std::size_t I, std::size_t...Is>
struct reverse<std::index_sequence<I, Is...>> {
using subset = typename reverse<std::index_sequence<Is...>>::type;
using type = typename append<subset, I>::result;
};
}
template<class Sequence>
using reverse = typename detail::reverse<Sequence>::type;
template
<
class Tuple,
class F
>
auto forward_over_tuple(F &&f, Tuple &&tuple) {
using tuple_type = std::decay_t<Tuple>;
constexpr auto size = std::tuple_size<tuple_type>::value;
return detail::invoke_over_tuple(std::forward<F>(f),
std::forward<Tuple>(tuple),
std::make_index_sequence<size>());
};
template
<
class Tuple,
class F
>
auto reverse_over_tuple(F &&f, Tuple &&tuple) {
using tuple_type = std::decay_t<Tuple>;
constexpr auto size = std::tuple_size<tuple_type>::value;
return detail::invoke_over_tuple(std::forward<F>(f),
std::forward<Tuple>(tuple),
reverse<std::make_index_sequence<size>>());
};
int main()
{
auto t = std::make_tuple("1", 2, 3.3, 4.4, 5, 6, "7");
forward_over_tuple([](auto &&x) { std::cout << x << " "; }, t);
std::cout << std::endl;
reverse_over_tuple([](auto &&x) { std::cout << x << " "; }, t);
std::cout << std::endl;
}