简体   繁体   中英

std::tuple types to another ones

How can I iterate over a tuple (using C++11)? I tried the following:

for(int i=0; istd::tuple_size<T...::value; ++i) std::get(my_tuple).do_sth(); but this doesn't work:

Error 1: sorry, unimplemented: cannot expand 'Listener...' into a fixed-length argument list. Error 2: i cannot appear in a constant expression.

So, how do I correctly iterate over the elements of a tuple?

Using a meta function like

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

you can create a type trait like

template <template <typename> typename Transformer, typename Tuple>
using transform_types_t = decltype(transform_types<Transformer>(std::declval<Tuple>()));

and then you would use it like

using transformed_tuple = transform_types_t<Transforming, TUPLE>;

and now transformed_tuple is a std::tuple<int, float, int, short int, float, float> . You can see it working in this live example

C++ templates have a kind of parameter called template template parameter which is a template parameter which is itself a template. These types of parameters can be used to provide templates like 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>);

You can do it like this:

#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>;
}

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