Search code examples
c++c++11templatesusing-declaration

how to remove duplicate template arguments in template declaration


For conciseness sake I would like to name a template argument only once in its explicit instantiation, but I am getting a compiler error. I am attempting to use the C++ syntax as described in cppreference under Type alias, alias template. Here is my sample code:

struct M {};

template< typename T1 >
struct S {};

template< typename T2, typename T3 > 
struct N {};

// type alias used to hide a template parameter (from cppreference under 'Type alias, alias template')
//template< typename U1, typename U2 >
//using NN = N< U1, U2< U1 > >; // error: attempt at applying alias syntax: error C2947: expecting '>' to terminate template-argument-list, found '<'

int main()
{
  N< M, S< M > > nn1; // OK: explicit instantiation with full declaration, but would like to not have to use M twice
  // NN< M, S > nn2; // desired declaration, error: error C2947: expecting '>' to terminate template-argument-list, found '<'

  return 0;
}

What is the problem here?


Solution

  • typename U2 is a typename, not a template. Thus, U2< U1 > doesn't make sense. Replace it with a template template parameter:

    template< typename U1, template<typename> class U2 >
    using NN = N< U1, U2< U1 > >;
    

    demo