6

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.

spraff
  • 32,570
  • 22
  • 121
  • 229
  • You misinterpreted the rule. Once you specialized a member function you cannot specialize a class on the same type, not on all other types. – Gene Bushuyev Aug 03 '11 at 17:22

2 Answers2

5

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;}
};
Chad
  • 18,706
  • 4
  • 46
  • 63
  • 1
    This is an ordinary redefinition error, nothing to do with templates. Is this why I'm confused? Simply because it got mentioned in the context of templates? Good grief... – spraff Aug 03 '11 at 17:02
  • In this context you can think of it like that yes. If you remove the implementation of `foo()` in the specialization of `C`, it will simply treat the partial specialization as the implementation of this function. – Chad Aug 03 '11 at 17:07
4

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>

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Please see my updated question. I've now specialised one function (bar) for `C` and given a full specialisation for `C` -- doesn't ***this*** contradict the textbook? – spraff Aug 03 '11 at 16:59
  • No, your "partial specialization" of C::foo() is simply actually the definition for the fully specialized version. See my answer below for details. – Chad Aug 03 '11 at 17:04