Patrick Wright
Patrick Wright

Reputation: 1663

Extract list of types from std::tuple for template class

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;

Upvotes: 7

Views: 1681

Answers (2)

Ch3steR
Ch3steR

Reputation: 20669

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

Upvotes: 2

康桓瑋
康桓瑋

Reputation: 42756

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;

Upvotes: 8

Related Questions