Search code examples
c++c++11variadic-templatestemplate-meta-programmingtype-traits

How to apply a meta function to the template types of a variadic template class?


Lets assume I have numerous atomic structs each having an inner_type:

struct Atomic1{
    using inner_type = int;
};
struct Atomic2{
    using inner_type = double;
};
struct Atomic3{
    using inner_type = bool;
};
...

My client class is a variadic template that can use 1 or more of the above atomic classes:

template<class ...AtomicTypeArgPack>
class MyclassAcceptingAtomicTypes;

I have have a related generic class that accepts Atomic*::inner_type as template parameters:

template<class ...InnerTypeArgPack>
class MyclassAcceptingInnerTypes;

My specific api class is defined but specifying a couple of template types:

using my_first_class_t = MyclassAcceptingAtomicTypes<Atomic1, Atomic2>;

for each specific class, I also have another class of inner types:

using my_first_class_inner_types_t = MyclassAcceptingInnerTypes<Atomic1::inner_type ,  Atomic2::inner_type >;

Is there is way to automatically generate the second type (i.e. my_first_class_inner_types_t) from the first declaration (my_first_class_t) using template meta programming / meta functions?


Solution

  • Is there is way to automatically generate the second type (i.e. my_first_class_inner_types_t) from the first declaration (my_first_class_t) using template meta programming / meta functions?

    Do you mean something as follows ?

    template <typename ... Ts>
    constexpr auto foo (MyclassAcceptingAtomicTypes<Ts...> const &)
       -> MyclassAcceptingInnerTypes<typename Ts::inner_type...>;
    
    template <typename T>
    using bar = decltype(foo(std::declval<T>()));
    

    You can verify that

    static_assert( std::is_same<bar<my_first_class_t>,
                                my_first_class_inner_types_t>{}, "!" );