无法专门化结构
为什么这不起作用?
template <class T>
struct Low;
template <>
struct Low<int> {};//Here I'm trying to specialize for int
int main()
{
Low<1> a;
}
Why this doesn't work?
template <class T>
struct Low;
template <>
struct Low<int> {};//Here I'm trying to specialize for int
int main()
{
Low<1> a;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
低a;
将起作用 - 您的模板采用类型而不是积分参数。Low<int> a;
will work - Your template takes a type not an integral argument.您的类模板
Low
需要 TYPE,而不是 INTEGRAL VALUE!如果您想使用这种方式,则必须将类模板定义为:
这允许您编写
Low<1>
,Low<2>
,Low<400>
等。如果您将
Low
定义为,那么您必须在实例化它时提供类型。例如,
Low
、Low
等。因此请注意它们在每种情况下定义方式以及实例化方式的差异!
Your class template
Low
expects TYPE, not INTEGRAL VALUE!If you want to use that way, you've to define your class template as:
This allows you to write
Low<1>
,Low<2>
,Low<400>
, etc.If you define
Low
as,Then you've to provide a type when instantiating it. For example,
Low<char>
,Low<unsigned int>
, etc.So notice the difference how they're defined in each case, and how they are instantiated!
Low<1>
和Low
之间存在差异。您需要为
Low<1>
编写专门化,但这是不可能的,因为原始模板采用类型作为第一个参数而不是值。There is a difference between
Low<1>
andLow<int>
.You will need to write a specialization for
Low<1>
, but that is not possible since the original template takes a type as the first parameter not a value.