Partial template specialization



 DEVELOP > c-Plus-Plus > Partial template specialization

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Martin"
Date: 28 Jun 2007 09:46:33 AM
Object: Partial template specialization
I'm trying to make a partial specialization of a class of mine. Can
someone please tell me what's wrong with the following code? GCC gives
me the error "invalid use of undefined type "class X<int, B>"".
template <class A, class B>
class X
{
public:
void f();
};
template <>
void X<int, int>::f()
{ cout << "int-int";}
template <class B>
void X<int, B>::f() //error on this line
{ cout << "int-B"; }
int main( int argc, char** argv )
{
X<int, double> x;
x.f();
return 0;
}
.

User: "Victor Bazarov"

Title: Re: Partial template specialization 28 Jun 2007 09:58:36 AM
Martin wrote:

I'm trying to make a partial specialization of a class of mine.

No, you are not. You are confused.

Can
someone please tell me what's wrong with the following code? GCC gives
me the error "invalid use of undefined type "class X<int, B>"".

template <class A, class B>
class X
{
public:
void f();
};

template <>
void X<int, int>::f()
{ cout << "int-int";}

template <class B>
void X<int, B>::f() //error on this line
{ cout << "int-B"; }

The preceding definition (of 'X<int,B>::f') is NOT a partial specialisation
of the class template X. It's a definition of a member of the
*non-existent*
partial specialisation of 'X'. And that's prohibited. If you need to
define
a partial specialisation of 'X', do exactly that:
template<class B>
class X<int, B> {
public:
void f();
};
and *only then* define the member.


int main( int argc, char** argv )

Don't declare arguments you're not going to use.

{
X<int, double> x;
x.f();
return 0;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER