C++:模板的 Typedef class

C++: Typedef of template class

我正在尝试创建 typedef 我拥有的向量 class。我在 SO 上发现了类似的问题,但他们关注的是 classes 是接受不同类型数据的模板,而我的 class 是基于整数的模板。

所以,我的 class 是这样定义的:

namespace sc_dt {
  template <int W> class sc_bv { ... }; //meaning systemc_bit_vector
}

而且我想使用 typedef,这样我就不必每次都输入 sc_dt::。然而,通过使用这个:

typedef sc_dt::sc_bv<int> sc_vector;

我收到以下错误:

Type/value mismatch at argument 1 in template argument list

我该如何解决这个问题?

typedefs 无法模板化。但是,usings可以(这样就达到了预期的效果):

template<int W>
using sc_vector = sc_dt::sc_bv<W>;
namespace sc_dt {
  template <int W> class sc_bv { ... } //meaning systemc_bit_vector
}

有一个非类型模板参数。当你实例化一个 sc_bv 的对象时,你需要给它一个 int 常量,比如

sc_dt::sc_bv<2> foo;

如您所见,它不同于

typedef sc_dt::sc_bv<int> sc_vector;

你给它一个类型,而不是一个值。

如果您知道要为 sc_vector 使用什么值,那么您可以使用

typedef sc_dt::sc_bv<the_value_that_you_want_to_use> sc_vector;

或者如果您只是想让 sc_vector 成为 class 模板的新名称,那么您可以使用像

这样的别名模板
template<int value>
using sc_vector = sc_dt::sc_bv<value>;

然后您可以像

一样使用sc_vector
sc_vector<some_value> foo;

如果您不想每次都输入名称空间 sc_dt::using 名称空间或

using sc_dt::sc_bv;