template declaration and definition

R

ruebezaehler

Hello,

I should separate the definition and declaration of template code. This
works fine for non-specialized templates. But I do not know how to do
this for specialized templates.

Example:

template<typename T>
class C_B
{
public:
T var;
public:
C_B( void );
virtual ~C_B( void );
};

template<typename T>
C_B<T>::C_B( void ) : var(0){ return;}

template<typename T>
C_B<T>::~C_B( void ) { return;}


// specialized for char*
template<>
class C_B<char*>
{
public:
static const int MAXCHARS = 50;
char* var;
public:
C_B( void );
virtual ~C_B( void );
};

// this won't compile
template<>
C_B<char*>::C_B( void )
: var( new char[MAXCHARS+1] )
{
var[0]='\0';
return;
}
 
R

ruebezaehler

Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
Please, please, drop the habit of putting something between parentheses
where nothing is _meant_ to be there. It's C-ism, it's ugly.

Please, please, drop the habit ...: I need 3 pleases to do that.
 
R

ruebezaehler

Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
ruebezaehler said:
I should separate the definition and declaration of template code. This
works fine for non-specialized templates. But I do not know how to do
this for specialized templates.

Example:

template<typename T>
class C_B
{
public:
T var;
public:
C_B( void );
virtual ~C_B( void );

Please, please, drop the habit of putting something between parentheses
where nothing is _meant_ to be there. It's C-ism, it's ugly.
};

template<typename T>
C_B<T>::C_B( void ) : var(0){ return;}

template<typename T>
C_B<T>::~C_B( void ) { return;}


// specialized for char*
template<>
class C_B<char*>
{
public:
static const int MAXCHARS = 50;
char* var;
public:
C_B( void );
virtual ~C_B( void );
};

// this won't compile

"Won't compile" - what does it mean? What error message do you get?
template<>
C_B<char*>::C_B( void )
: var( new char[MAXCHARS+1] )
{
var[0]='\0';
return;
}

I think you're mixing two concepts. The 'template<>' syntax (verbatim)
starts a declaration/definition of a full specialisation of a template.
You already got that when you fully specialised class C_B template. Now,
when you deed to define the already specialised constructor from that
class, you should omit the 'template<>' construct and simply write

C_B<char*>::C_B() ...

Try it, let us know how it turns out.

V

undefined reference to `vtable for C_B<char*>'
 
J

jason.cipriani

Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:



undefined reference to `vtable for C_B<char*>'

You need to define the specialized ~C_B's destructor as well, just as
you need to define any class's destructor or other members if you
declare them. Explicitly specializing the entire class is effectively
creating a new, completely independent class. You don't get any of the
stuff from the default template.

Jason
 
R

ruebezaehler

Am Fri, 23 May 2008 09:19:12 -0700 schrieb (e-mail address removed):
You need to define the specialized ~C_B's destructor as well, just as
you need to define any class's destructor or other members if you
declare them. Explicitly specializing the entire class is effectively
creating a new, completely independent class. You don't get any of the
stuff from the default template.

Jason

Hallo Jason,

thank You for that hint. I overlooked the forgotten definition of the ~
and =.

It works.

best regards, ruebezaehler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top