为什么显式专业化和部分专业化之间的语法存在差异?

发布于 2024-12-25 19:33:07 字数 406 浏览 3 评论 0原文

示例:

template <typename T, typename U>
struct A {
    void Print() {}
};

template <>
void A<int, float>::Print() {} // Okay                   

template <typename T>
void A<T, char>::Print() {} // Will produce error

问题:

我知道您必须在上面的代码中定义类模板部分特化才能使其工作,并且我还从标准中知道 类模板部分特化的成员与主模板 (§ 14.5.5.3)。但是,为什么显式特化和部分特化之间的语法存在差异?

Example:

template <typename T, typename U>
struct A {
    void Print() {}
};

template <>
void A<int, float>::Print() {} // Okay                   

template <typename T>
void A<T, char>::Print() {} // Will produce error

Question:

I know that you have to define the class template partial specialization in the above code for it to work and I also know from the standard that The members of the class template partial specialization are unrelated to the members of the primary template (§ 14.5.5.3). However, why the difference in syntax between a explication specialization and a partial specialization?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

樱娆 2025-01-01 19:33:07

您不能部分地专门化函数模板,而只能完全地专门化。

第一个实例利用了类模板的成员函数本身就是函数模板这一事实,因此限制适用于它们。

当您部分特化类模板时,您将拥有一个全新的类模板,您必须重新定义它。

You cannot specialize function templates partially, only fully.

The first instance makes use of the fact that member functions of class templates are themselves function templates, and thus the restriction applies to them.

When you partially-specialize the class template, you have an entirely new class template, which you have to define anew.

不弃不离 2025-01-01 19:33:07
template <typename T>
void A<T, char>::Print() {} // Will produce error

您正在:

  1. 重新定义一个函数(在声明 void Print() {} 时它已经被定义,您会看到有 {}.
  2. 以及一个模板参数列表与声明不匹配: templatevoid Print()

事实上,即使在声明函数时没有定义该函数,仍然会出现错误;你的声明和定义不匹配,编译器不会能够找到原始模板的定义或专用模板的声明与

结构相关的函数的专用模板函数也必须具有专用结构,此代码有效:

template <typename T, typename U>
struct A {
    void Print() {}
};

template <>
void A<int, float>::Print() {} // Okay                   


template <typename T>
struct A<T,char>
{
    void Print();
};

template <typename T>
void A<T,char>::Print() {}

因为模板函数已在中声明。它是模板结构。

template <typename T>
void A<T, char>::Print() {} // Will produce error

You are:

  1. re-defining a function (it has already been defined when declared void Print() {}, you see there are {}.
  2. with a template argument list that doesn't match the declaration: template <typename T, typename U> void Print()

In fact, even if you haven't defined the function when declared it, you will still have an error since your declaration and definition do not match, the compiler will not be able to find a definition for the original template, or a declaration for the specialized template.

A specialized template function for a function that is related to a struct must have a specialized struct as well, This code works:

template <typename T, typename U>
struct A {
    void Print() {}
};

template <>
void A<int, float>::Print() {} // Okay                   


template <typename T>
struct A<T,char>
{
    void Print();
};

template <typename T>
void A<T,char>::Print() {}

Because template function has been declared in it's template struct.

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