Home > Software design >  Alias that transforms a template type to the same type but templated on something different
Alias that transforms a template type to the same type but templated on something different

Time:05-10

I have a

template <typename T, typename U>
struct A;

and a

template <typename U>
struct B;

How do I make an alias that transforms an A<T, U> type to a A<T, B<U>> type? i.e. something that looks like this:

template <typename TypeA>
using ModifiedTypeA = TypeA<T, B<U>>; // <--- I don't know how to get T and U from TypeA

where ModifiedTypeA only needs to be templated on TypeA?

I was thinking that the above could be achieved if TypeA is always guaranteed to have member aliases

template <typename T_, typename U_>
struct A
{
  using T = T_;
  using U = U_;
};

and then do

template <typename TypeA>
using ModifiedTypeA = TypeA<typename TypeA::T, B<typename TypeA::U>>;

But is there another cleaner way that doesn't make the above assumption?

CodePudding user response:

Try

template <typename TypeA, template<typename> typename TemplateB>
struct ModifiedTypeAWtihImpl;

template <template<typename, typename> typename TemplateA, 
          typename T, typename U,
          template<typename> typename TemplateB>
struct ModifiedTypeAWtihImpl<TemplateA<T, U>, TemplateB> {
  using type = TemplateA<T, TemplateB<U>>;
};

template <typename TypeA, template<typename> typename TemplateB>
using ModifiedTypeAWtih = typename ModifiedTypeAWtihImpl<TypeA, TemplateB>::type;

Demo

  • Related