这本教科书有错吗?专门化某些成员函数,但不专门化其他成员函数

发布于 2024-11-28 00:19:23 字数 1030 浏览 1 评论 0原文

我正在阅读 Vandevoorde 和 Josuttis 的“C++ 模板完整指南”(顺便说一句,这看起来相当不错)。这种说法(第 3.3 节)似乎是错误的并且不在已发布的勘误表中

如果您专门化一个类模板,则还必须专门化所有成员函数。虽然可以专门化单个成员函数,但一旦这样做,就不能再专门化整个类。

然而下面的代码在 gcc 上编译 模板

<typename T>
struct C {
    T foo ();
    T bar ();
};

template <>
struct C<int> {
    int foo ();
    int bar () {return 4;}
};

template <typename T>
T C<T> :: foo () {return 0;}

template <typename T>
T C<T> :: bar () {return 1;}

int C<int> :: foo () {return 2;}

template <>
float C<float> :: bar () {return 3;}

#include <cassert>

int main () {
    C<int> i;
    C<float> f;
    assert (2 == i .foo ());
    assert (0 == f .foo ());
    assert (4 == i .bar ());
    assert (3 == f .bar ());
}

我有专门的 C::fooC::bar 所以教科书是错误的,gcc 超出了标准,还是我误解了整个情况?

谢谢。

I'm reading Vandevoorde and Josuttis's "C++ Templates The Complete Guide" (which seems pretty good, by the way). This claim (section 3.3) seems to be wrong and is not in the published errata:

If you specialise a class template, you must also specialise all member functions. Although it is possible to specialise a single member function, once you have done so, you can no longer specialise the whole class.

Yet the following compiles on gcc
template

<typename T>
struct C {
    T foo ();
    T bar ();
};

template <>
struct C<int> {
    int foo ();
    int bar () {return 4;}
};

template <typename T>
T C<T> :: foo () {return 0;}

template <typename T>
T C<T> :: bar () {return 1;}

int C<int> :: foo () {return 2;}

template <>
float C<float> :: bar () {return 3;}

#include <cassert>

int main () {
    C<int> i;
    C<float> f;
    assert (2 == i .foo ());
    assert (0 == f .foo ());
    assert (4 == i .bar ());
    assert (3 == f .bar ());
}

I have specialised C<int>::foo and C<float>::bar so is the textbook wrong, is gcc going beyond the standard, or am I misunderstanding the whole situation?

Thanks.

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

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

发布评论

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

评论(2

沉鱼一梦 2024-12-05 00:19:23

你不能这样做:

template <typename T> struct C
{
   T foo ()     { return 0;}
   T bar ()     { return 1;}
};

// partial specialization of foo on C<int>
template <>
int C<int> :: foo () {return 2;}

// partial specialization of bar on C<float>
template <>
float C<float> :: bar () {return 3;}

// will not compile, C<int> already partially specialized
template <>
struct C<int>
{
   int foo() {return 10;}
   int bar() {return 10;}
};

You cannot do this:

template <typename T> struct C
{
   T foo ()     { return 0;}
   T bar ()     { return 1;}
};

// partial specialization of foo on C<int>
template <>
int C<int> :: foo () {return 2;}

// partial specialization of bar on C<float>
template <>
float C<float> :: bar () {return 3;}

// will not compile, C<int> already partially specialized
template <>
struct C<int>
{
   int foo() {return 10;}
   int bar() {return 10;}
};
坏尐絯℡ 2024-12-05 00:19:23

不,这本书没有错。恐怕你的理解是:)

在这种情况下,你有专门的只有1个成员函数 - foo for C;bar 用于 C

现在您无法显式特化 CC。但您可以专门化 C

No, the book isn't wrong. Your understanding is, I am afraid :)

In this case you have specialized only 1 member function - foo for C<int> and bar for C<float>

Now you can't explicitly specialize C<int> or C<float>. But you can specialize C<char>

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