6

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> >
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
Nick
  • 9,962
  • 4
  • 42
  • 80

1 Answers1

13

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Wow... Is that standard feature? – Nick Aug 03 '20 at 20:14
  • 2
    @Nick Yes [indeed](https://en.cppreference.com/w/cpp/language/template_parameters#Template_template_arguments), and here [more read from std](http://eel.is/c++draft//temp.arg.template) – JeJo Aug 03 '20 at 20:16