C++具有恒定值的模板专业化
是否有一种简单的方法来定义 C++ 模板类的部分特化(给定模板参数之一的数字常量)?我试图仅为某些类型的模板组合创建特殊的构造函数:
template <typename A, size_t B> class Example
{
public:
Example() { };
A value[B];
};
template <typename A, 2> class Example
{
public:
Example(b1, b2) { value[0] = b1; value[1] = b2; };
};
此示例将无法编译,并在第二个定义中返回错误数字常量之前的预期标识符
。
我已经浏览了这里和其他地方的许多示例,但大多数似乎都围绕着类型而不是常量的专门化。
编辑:
寻找一种编写有条件使用的构造函数的方法,其功能如下:
template <typename A, size_t B> class Example
{
public:
// Default constructor
Example() { };
// Specialized constructor for two values
Example<A,2>(A b1, A b2) { value[0] = b1; value[1] = b2; };
A foo() {
A r;
for (size_t i = 0; i < b; ++b)
r += value[i];
return r;
}
// Hypothetical specialized implementation
A foo<A, 2>() {
return value[0] + value[1];
}
A value[B];
};
Is there a straightforward way for defining a partial specialization of a C++ template class given a numerical constant for one of the template parameters? I'm trying to create special constructors for only certain kinds of template combinations:
template <typename A, size_t B> class Example
{
public:
Example() { };
A value[B];
};
template <typename A, 2> class Example
{
public:
Example(b1, b2) { value[0] = b1; value[1] = b2; };
};
This example won't compile, returning an error Expected identifier before numeric constant
in the second definition.
I've had a look through a number of examples here and elsewhere, but most seem to revolve around specializing with a type and not with a constant.
Edit:
Looking for a way to write a conditionally used constructor, something functionally like this:
template <typename A, size_t B> class Example
{
public:
// Default constructor
Example() { };
// Specialized constructor for two values
Example<A,2>(A b1, A b2) { value[0] = b1; value[1] = b2; };
A foo() {
A r;
for (size_t i = 0; i < b; ++b)
r += value[i];
return r;
}
// Hypothetical specialized implementation
A foo<A, 2>() {
return value[0] + value[1];
}
A value[B];
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要将特化放在正确的位置:
如果您想创建子类:
专门化 typedef 的行为与专门化整数参数的行为类似。
You need to put the specialization in the correct place:
If you want to create a subclass:
The behavior for specializing on typedefs is similar to the behavior for specializing on an integer parameter.
我认为这可能有效:
抱歉,我只是从我的测试项目中复制并粘贴它。在某种程度上,它并不是真正的“专业化”,它只是调用专门函数的重载。我不确定这是否是您想要的,而且在我看来这不是很优雅。
I think this might work:
Sorry, I just copy and pasted it from my test project. It's not really "specialization" in a way, it just calls overloads to specialized functions. I'm not sure if this is what you want and imo this isn't very elegant.
如果没记错的话,它应该更像是:
尽管如此,我不认为这是完全允许的——没有任何定义
b1
和/或b2
的类型在专业版本中。编辑[基于编辑的问题]:是的,模板专业化产生了一种新类型,该类型与其专业化的基础并不真正相关。特别是,两者不共享任何实现。您不能(通过专门化类模板)生成使用两个不同构造函数之一的单一类型,具体取决于非类型参数的值。
If memory serves, it should be more like:
I don't think this is quite allowable as-is though -- there's nothing defining the types of
b1
and/orb2
in the specialized version.Edit [based on edited question]: Yes, a template specialization produces a new type that's not really related to the base from which it's specialized. In particular, the two do not share any of the implementation. You can't (by specializing a class template) produce a single type that uses one of two different ctors, depending on the value of a non-type parameter.
您可以尝试这样的操作:
http://www.ideone.com/wDcL7 上的工作示例
模板中 如果您不使用方法,则它不会存在于编译的代码中,因此此解决方案不应使可执行文件更大,因为您无法与某些专用类一起使用的构造函数(例如不应包含
Example
ExampleExample(A b1, A b2)
ctor)。You can try something like this:
Working example on http://www.ideone.com/wDcL7
In templates if you are not using method it won't exists in compiled code, so this solution shouldn't make executable bigger because of ctors you can't use with some specialized class (for example
Example<int, 1>
should not haveExample(A b1, A b2)
ctor).如果您的目标是只需要重写专业化中的一些方法/构造函数,那么可以考虑使用通用基类来保存所有
Example
模板的通用实现,这样您就不必重写它适用于您提出的每个专业领域。例如:
If you're goal is to only have to override a few methods/constructors in your specializations then maybe consider a generic base class to hold the common implementation for all
Example
templates so you don't have to rewrite it in every specialization you come up with.For example:
这在我的 Linux 机器上有效
That worked on my linux machine with