成员模板专业化及其范围

发布于 2024-07-17 15:58:46 字数 592 浏览 9 评论 0原文

在我看来,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往昔成烟 2024-07-24 15:58:46

我明白你想要做什么,但你做得不对。 尝试这个 :

struct Base{};
struct Derived{};

// Original definition of Kind
// Will yield an error if Kind is not used properly
template<typename WhatToDo, typename T>
struct Kind
{
};

// definition of Kind for Base selector
template<typename T>
struct Kind<Base, T>
{
  typedef T type;
};

// Here is the inheritance you wanted
template<typename T>
struct Kind<Derived, T> : Kind<Base, T>
{
};

// ... and the specialization for float
template<>
struct Kind<Derived, float>
{
  typedef double type;
};

I get what you're trying to do, but you are not doing it right. Try this :

struct Base{};
struct Derived{};

// Original definition of Kind
// Will yield an error if Kind is not used properly
template<typename WhatToDo, typename T>
struct Kind
{
};

// definition of Kind for Base selector
template<typename T>
struct Kind<Base, T>
{
  typedef T type;
};

// Here is the inheritance you wanted
template<typename T>
struct Kind<Derived, T> : Kind<Base, T>
{
};

// ... and the specialization for float
template<>
struct Kind<Derived, float>
{
  typedef double type;
};
不美如何 2024-07-24 15:58:46

我的问题是为什么不允许这样做?

从我的草案副本来看,以下内容提出了上述限制:


类模板、类模板的成员或类成员的显式专业化声明
template,明确专门化的类的名称应为 simple-template-id。

解决方法是专门化封闭类。

My question is why isn't it allowed?

From my copy of the draft it appears that the following puts the above restriction:

In
an explicit specialization declaration for a class template, a member of a class template or a class member
template, the name of the class that is explicitly specialized shall be a simple-template-id.

The workaround is to specialize the enclosing class.

┾廆蒐ゝ 2024-07-24 15:58:46

我将“忽略”标准规范并尝试逻辑论证:

如果您有两个类:

class A
{
   struct S { };

};

class B: public A
{
   struct S { };
};

A::S 和 B::S 是两种不同的类型。 将逻辑扩展到模板特化,当您尝试通过派生类中的内部类来特化基类中声明的内部类时,您实际上是在尝试定义具有相同名称(但命名范围不同)的不同类型。

I will "ignore" the standard specifications and try a logical argument:

If you have two classes:

class A
{
   struct S { };

};

class B: public A
{
   struct S { };
};

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文