Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid this kind of code repetition?

In order to avoid code repetition, I need to do something like this (in my real code I have much more complex types similar to T1 and T2):

template <class T1, class T2>
struct A 
{};

template <class T1, class T2>
struct B 
{};

template <class X>
struct C 
{
   using p1 = int;
   using p2 = char;

   using some = X<p1, p2>;
};

int main()
{
   C<A> o1; // must produce C<A<int,char> >
   C<B> o2; // must produce C<B<int,char> >
}
like image 775
Nick Avatar asked Aug 03 '20 20:08

Nick


1 Answers

Your class C needs to use a template template parameter in order to accept A and B as input to its own template so it can then pass parameters to them, eg:

template <template<typename T1, typename T2> class X>
struct C 
{
   using p1 = int;
   using p2 = char;

   using some = X<p1, p2>;
};

Now you can do this:

C<A> o1; // produce C<A<int,char> >
C<B> o2; // produce C<B<int,char> >

See a demo

like image 69
JeJo Avatar answered Oct 24 '22 16:10

JeJo