【问题标题】:how to remove duplicate template arguments in template declaration如何在模板声明中删除重复的模板参数
【发布时间】:2017-04-03 11:00:51
【问题描述】:

为了简洁起见,我只想在模板参数的显式实例化中命名一次,但出现编译器错误。我正在尝试使用 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;
}

这里有什么问题?

【问题讨论】:

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


    【解决方案1】:

    typename U2 是类型名,而不是模板。因此,U2&lt; U1 &gt; 没有意义。将其替换为模板模板参数:

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

    demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 2021-12-21
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      相关资源
      最近更新 更多