std::tuple 类型到另一个

时间:2021-05-06 17:31:47

标签: c++ generics

如何迭代元组(使用 C++11)?我尝试了以下方法:

for(int i=0; istd::tuple_size

错误 1:抱歉,未实现:无法将“侦听器...”扩展为固定长度的参数列表。 错误 2:i 不能出现在常量表达式中。

那么,如何正确迭代元组的元素?

3 个答案:

答案 0 :(得分:3)

使用元函数如

template <template <typename> typename Transformer, typename... Ts> 
auto transform_types(std::tuple<Ts...>) -> std::tuple<typename Transformer<Ts>::type...>;

你可以创建一个像

这样的类型特征
template <template <typename> typename Transformer, typename Tuple>
using transform_types_t = decltype(transform_types<Transformer>(std::declval<Tuple>()));

然后你会像这样使用它

using transformed_tuple = transform_types_t<Transforming, TUPLE>;

现在 transformed_tuplestd::tuple<int, float, int, short int, float, float>。你可以看到它在这个 live example

答案 1 :(得分:1)

C++ 模板有一种参数叫做 template template parameter,它是一个模板参数,它本身就是一个模板。这些类型的参数可用于提供 Transforming<T> 之类的模板。

Example :

#include <tuple>

// Original trait
template<typename ...> struct Transforming;
template<typename T> struct Transforming<T> { using type = T; };
template<> struct Transforming<char> { using type = int; };
template<> struct Transforming<long> { using type = int; };
template<> struct Transforming<double> { using type = float; };

// T is a template template parameter
// It is a `class` template which has a single `class` template parameter
// Tuple is the tuple of types to transform
template<template<class> class T, class Tuple>
struct transform_types;

// Specialize for `std::tuple` to extract argument types
template<template<class> class T, class ... A>
struct transform_types<T, std::tuple<A...>>
{
    // Make a new `std::tuple` with the transformed types
    // Expand the parameter pack using the template class `T`
    using type = std::tuple<typename T<A>::type...>;
};

template<template<class> class T, class Tuple>
using transform_types_t = typename transform_types<T, Tuple>::type;

// Demonstration
using input_tuple = std::tuple<int, float, char, short, double, double>;
using transformed = transform_types_t<Transforming, input_tuple>;
using expected = std::tuple<int, float, int, short, float, float>;

#include <type_traits>
static_assert(std::is_same_v<transformed, expected>);

答案 2 :(得分:0)

你可以这样做:

#include <iostream>
#include <type_traits>
#include <tuple>

template<typename ...> struct Transforming;
template<typename T> struct Transforming<T> { using type = T; };
template<> struct Transforming<char> { using type = int; };
template<> struct Transforming<long> { using type = int; };
template<> struct Transforming<double> { using type = float; };

template <template<typename...> class Transform,typename T>
struct Transformed;

template <template<typename...> class Transform,typename...Ts>
struct Transformed<Transform, std::tuple<Ts...>> {
    using type = std::tuple<typename Transform<Ts>::type ...>;
};


int main() {
    using t = std::tuple<char,long,double>;
    using transformed = Transformed<Transforming,t>::type;
    using expected = std::tuple<int,int,float>;
    std::cout << std::is_same_v< transformed,expected>;
}
相关问题