Extract list of types from std::tuple for template class
Asked Answered
R

2

6

Suppose I have the following class

class Example {
public:
    using value_type = std::tuple<
        uint8_t,
        uint8_t,
        uint16_t
    >;

private:
    value_type _value;
};

Now, I want to be able to create another class based upon this type that wraps each of the classes types in another type. Based upon Wrapping each type in a variadic template in a templated class, I know I can accomplish half my goal via:

template <typename T>
class Wrapper;

template <typename ... ARGS>
class ExampleWrapper {
private:
    std::tuple<Wrapper<ARGS>...> _args;
};

However, what I can't figure out is how to get ARGS if all I know is T, where T is Example. I would like to be able to use ExampleWrapper as follows:

ExampleWrapper<Example> myWrapper;
Rehearse answered 4/2, 2022 at 17:9 Comment(1)
godbolt.org/z/6vGK9PEhYKoehler
A
8

You can use template partial specialization to get ARGS:

template <typename T>
class Wrapper;

template <typename Tuple>
class ExampleWrapper;

template <typename ... ARGS>
class ExampleWrapper<std::tuple<ARGS...>> {
private:
    std::tuple<Wrapper<ARGS>...> _args;
};

Then:

ExampleWrapper<Example::value_type> myWrapper;
Allergen answered 4/2, 2022 at 17:17 Comment(0)
D
1

Make a static function to return the desired type and use decltype on it.

template <typename T>
class Wrapper{};

template<class T>
class ExampleWrapper {
    public:
    template<typename ...Args>
    static std::tuple<Wrapper<Args>...> fnc(std::tuple<Args...>);  
    using value_type = decltype( fnc( std::declval<typename T::value_type>() ) );

    private:
    value_type _args;  
};
// The type of `ExampleWrapper<Example>::_args` would be
// std::tuple<Wrapper<unsigned char>, Wrapper<unsigned char>, Wrapper<short unsigned int> >
}

There's no need to make the static function declaration and value_type alias public. I made them public just for the sake of demonstration.

Demo

Dumuzi answered 4/2, 2022 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.