By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

template declaration and definition

P: n/a
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;
}
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ruebezaehler wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2

P: n/a
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.
Jun 27 '08 #3

P: n/a
Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
ruebezaehler wrote:
>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*>'
Jun 27 '08 #4

P: n/a
On May 23, 12:00 pm, ruebezaehler <ruebezaeh...@web.dewrote:
Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
C_B<char*>::C_B() ...
Try it, let us know how it turns out.
V

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
Jun 27 '08 #5

P: n/a
Am Fri, 23 May 2008 09:19:12 -0700 schrieb ja************@gmail.com:
On May 23, 12:00 pm, ruebezaehler <ruebezaeh...@web.dewrote:
>Am Thu, 22 May 2008 08:09:30 -0400 schrieb Victor Bazarov:
C_B<char*>::C_B() ...
Try it, let us know how it turns out.
V

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
Hallo Jason,

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

It works.

best regards, ruebezaehler
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.