具有部分专用化的模板子类

template subclassing with partial specialization

本文关键字:子类 专用      更新时间:2023-10-16

考虑以下事项

template<typename Type, size_t Dimensions> 
struct Base 
{  
     Base(/*some args*/)  {   /*do something*/  }
};
template<size_t Dim>
class Derived : Base<double,Dim> {};

派生不会继承 Base 的构造函数,因为它表明 Base 是部分专用化,因此需要一些不同的构造函数。然而,在我的情况下并非如此。我主要希望它在不同情况下有不同的名称。我正在寻找与宏或以下解决方案不同的解决方案(如果存在)。

template<size_t Dim>
struct Derived
{
  typedef Base<double, Dim> Type;
}

主要是因为我不喜欢使用

Derived<n>::Type 

到处都是,不确定我想要

typedef Derived<n>

对于我使用的每一个 n。

试试这个:

template<std::size_t dim>
using foo =  typename derived<dim>::type;
//usage
foo<n> bar;

这利用了使用 语句的 C++11 模板

http://en.cppreference.com/w/cpp/language/type_alias