如何删除模板声明中的重复模板参数

时间:2016-11-19 18:45:32

标签: c++ c++11 templates using-declaration

为了简洁起见,我想在其显式实例化中仅将模板参数命名为一次,但是我收到编译器错误。我试图使用Type alias, alias template下的cppreference中描述的C ++语法。这是我的示例代码:

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;
}

这里有什么问题?

1 个答案:

答案 0 :(得分:3)

typename U2是一个类型名称,而不是模板。因此,U2< U1 >没有意义。将其替换为模板模板参数:

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

demo