成员模板专业化及其范围
在我看来,C++ 不允许在命名空间和全局范围之外的任何范围内进行成员模板专门化(MS VSC++ 错误 C3412)。 但对我来说,在派生类中专门化基类的主要成员模板是有意义的,因为这就是派生类所做的 - 专门化基类中的内容。 例如,考虑以下示例:
struct Base
{
template <class T>
struct Kind
{
typedef T type;
};
};
struct Derived : public Base
{
/* Not Allowed */
using Base::Kind;
template <>
struct Kind <float>
{
typedef double type;
};
};
int main(void)
{
Base::Kind<float>::type f; // float type desired
Derived::Kind<float>::type i; // double type desired but does not work.
}
我的问题是为什么不允许这样做?
It appears to me that C++ does not allow member template specialization in any scope other than namespace and global scope (MS VSC++ Error C3412). But to me it makes sense to specialize a base class's primary member template in the derived class because that is what derived classes do - specialize things in the base class. For instance, consider the following example:
struct Base
{
template <class T>
struct Kind
{
typedef T type;
};
};
struct Derived : public Base
{
/* Not Allowed */
using Base::Kind;
template <>
struct Kind <float>
{
typedef double type;
};
};
int main(void)
{
Base::Kind<float>::type f; // float type desired
Derived::Kind<float>::type i; // double type desired but does not work.
}
My question is why isn't it allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我明白你想要做什么,但你做得不对。 尝试这个 :
I get what you're trying to do, but you are not doing it right. Try this :
从我的草案副本来看,以下内容提出了上述限制:
解决方法是专门化封闭类。
From my copy of the draft it appears that the following puts the above restriction:
The workaround is to specialize the enclosing class.
我将“忽略”标准规范并尝试逻辑论证:
如果您有两个类:
A::S 和 B::S 是两种不同的类型。 将逻辑扩展到模板特化,当您尝试通过派生类中的内部类来特化基类中声明的内部类时,您实际上是在尝试定义具有相同名称(但命名范围不同)的不同类型。
I will "ignore" the standard specifications and try a logical argument:
If you have two classes:
A::S and B::S are two different types. Extending the logic to the template specializations, when you try to specialize an inner class declared in base class through an inner class in derived class, you actually are trying to define a different type, with the same name (but another naming scope).