If you want to define a member function from a partially specialized class,
you MUST predeclare the partial specialization, but if you want to define a
member function of a fully specialized class, you MUST NOT predeclare the
full specialization.

example


// full specialization

template <int a>
struct P {
void f();
};

// uncomment this and get an error
/*
template <>
struct P<0>
{
void f();
};
*/

template <>
void P<0>::f() {}




// partial specialization

template <int a, int b>
struct S {
void f();
};

// comment this and get an error
template <int a>
struct S<a, 0> {
void f();
};

template <int a>
void S<a, 0>::f () {}


Lucas/

Re: partial vs full (is this standard?) by John

John
Thu Nov 20 23:56:34 CST 2003

"Lucas Galfaso" <lgalfaso@NO_CAPSfd.com.ar> wrote in message
news:eyhpWy%23rDHA.3492@TK2MSFTNGP11.phx.gbl
> If you want to define a member function from a partially specialized
> class, you MUST predeclare the partial specialization, but if you
> want to define a member function of a fully specialized class, you
> MUST NOT predeclare the full specialization.
>
> example
>
>
> // full specialization
>
> template <int a>
> struct P {
> void f();
> };
>
> // uncomment this and get an error
> /*
> template <>
> struct P<0>
> {
> void f();
> };
> */
>
> template <>
> void P<0>::f() {}
>
>
>
>
> // partial specialization
>
> template <int a, int b>
> struct S {
> void f();
> };
>
> // comment this and get an error
> template <int a>
> struct S<a, 0> {
> void f();
> };
>
> template <int a>
> void S<a, 0>::f () {}
>
>
> Lucas/


I believe the rules for full specialisation are as follows.

You can fully specialise an entire class (or struct) or you can fully
specialise one or more member functions without specialising the class as a
whole. The distinction between these two is somewhat obscured when your
class has only a single member.

If you fully specialise a class, then you predeclare the specialised class
in its entirety (the specialised version may but need not have any members
in common with the non-specialised version) and then you must define EVERY
member function of the specialised class. In making these definitions, you
OMIT the template<> keyword from function definitions. Thus you would have:

template <int a>
struct P {
void f();
};

template <>
struct P<0>
{
void f();
};


// no template<> here
void P<0>::f() {}

If you only specialise one or more member functions, but leave the class
itself unspecialised, then you obviously don't need to give a declaration of
the specialised class. Thus you would have:

template <int a>
struct P {
void f();
};

template<>
void P<0>::f() {}


When it comes to partial specialisation, it is not possible to partially
specialise just a member function; you have to partially specialise the
class as a whole. Thus you must pre-declare the entire class and define all
of its members. Unlike in the case of full specialisation, you still need
the template <??> keyword when defining member functions because, of course,
there is still a template parameter involved in the class.



--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Re: partial vs full (is this standard?) by Lucas

Lucas
Fri Nov 21 00:21:57 CST 2003

thx
"John Carson" <donaldquixote@datafast.net.au> wrote in message
news:O3VUFR$rDHA.536@tk2msftngp13.phx.gbl...
> "Lucas Galfaso" <lgalfaso@NO_CAPSfd.com.ar> wrote in message
> news:eyhpWy%23rDHA.3492@TK2MSFTNGP11.phx.gbl
> > If you want to define a member function from a partially specialized
> > class, you MUST predeclare the partial specialization, but if you
> > want to define a member function of a fully specialized class, you
> > MUST NOT predeclare the full specialization.
> >
> > example
> >
> >
> > // full specialization
> >
> > template <int a>
> > struct P {
> > void f();
> > };
> >
> > // uncomment this and get an error
> > /*
> > template <>
> > struct P<0>
> > {
> > void f();
> > };
> > */
> >
> > template <>
> > void P<0>::f() {}
> >
> >
> >
> >
> > // partial specialization
> >
> > template <int a, int b>
> > struct S {
> > void f();
> > };
> >
> > // comment this and get an error
> > template <int a>
> > struct S<a, 0> {
> > void f();
> > };
> >
> > template <int a>
> > void S<a, 0>::f () {}
> >
> >
> > Lucas/
>
>
> I believe the rules for full specialisation are as follows.
>
> You can fully specialise an entire class (or struct) or you can fully
> specialise one or more member functions without specialising the class as
a
> whole. The distinction between these two is somewhat obscured when your
> class has only a single member.
>
> If you fully specialise a class, then you predeclare the specialised class
> in its entirety (the specialised version may but need not have any members
> in common with the non-specialised version) and then you must define EVERY
> member function of the specialised class. In making these definitions, you
> OMIT the template<> keyword from function definitions. Thus you would
have:
>
> template <int a>
> struct P {
> void f();
> };
>
> template <>
> struct P<0>
> {
> void f();
> };
>
>
> // no template<> here
> void P<0>::f() {}
>
> If you only specialise one or more member functions, but leave the class
> itself unspecialised, then you obviously don't need to give a declaration
of
> the specialised class. Thus you would have:
>
> template <int a>
> struct P {
> void f();
> };
>
> template<>
> void P<0>::f() {}
>
>
> When it comes to partial specialisation, it is not possible to partially
> specialise just a member function; you have to partially specialise the
> class as a whole. Thus you must pre-declare the entire class and define
all
> of its members. Unlike in the case of full specialisation, you still need
> the template <??> keyword when defining member functions because, of
course,
> there is still a template parameter involved in the class.
>
>
>
> --
> John Carson
> 1. To reply to email address, remove donald
> 2. Don't reply to email address (post here instead)
>