Home > Blockchain >  Extract list of types from std::tuple for template class
Extract list of types from std::tuple for template class

Time:02-05

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;

CodePudding user response:

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;

CodePudding user response:

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

template<typename T>
void test(T){ std::court << __PRETTY_FUNCTION__; }

int main(){
    ExampleWrapper<Example>::value_type a;
    test(a); /* void t(T) [with T = std::tuple<Wrapper<unsigned char>,
                                               Wrapper<unsigned char>,
                                               Wrapper<short unsigned int> >] 
              */
// The `T` in the above example is the type of `ExampleWrapper<Example>::_args`

}

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

  •  Tags:  
  • Related