typedef for template function

A

Andrey Balaguta

Hello, All!

I need to define new type for template function, but I've forgotten how to
use "template" in conjunction with "typedef" %) I've tried "typedef template
<class T> void (*TDataDestructor) (T Data)" and "template <class T> typedef
void (*TDataDestructor) (T Data)", both don't work with error "template must
be classes or functions" (bcc5.5). Point me to right answer :) Thanks in
advance

With best regards, Andrey Balaguta. E-mail: (e-mail address removed)
 
R

Rob Williscroft

Andrey Balaguta wrote in
Hello, All!

I need to define new type for template function, but I've forgotten
how to use "template" in conjunction with "typedef" %) I've tried
"typedef template <class T> void (*TDataDestructor) (T Data)" and
"template <class T> typedef void (*TDataDestructor) (T Data)", both
don't work with error "template must be classes or functions"
(bcc5.5). Point me to right answer :) Thanks in advance

What you've fogotten is that you can't do it (unless you have a time
machinr that is :).

There are currently no such things as template typedef's in C++.

Perhapse you could say why you think you need them, there is most
probably an alternative.

Rob.
 
A

Andrey Balaguta

Hello, Rob!
You wrote on 23 Nov 2003 19:05:19 GMT:

RW> What you've fogotten is that you can't do it (unless you have a time
RW> machinr that is :).


I think I did that some time ago, but I will not approve that :)


RW> Perhapse you could say why you think you need them, there is most
RW> probably an alternative.


I'm doing a balanced binary search tree (template-based). A node class has
an option to destruct children recursively (i.e. node destructor destructs
node's left and right children before die itself). So there is need to
destruct data automatically, which is kind of problem, because data may be a
pointer, so it must be deleted (automatically too). I've found only solution
is to set up addition variable (function pointer, that takes the only
parameter -- data) that destructs data (if it is null, node destructor
simply ignores data). Here is a chunk of code:

// here is a problem :)
template <class T>
typedef void (*TDataDestructor) (T data);

// ......

template <class T>
class TBinTreeNode
{
public :
//.........
T Data;
TDataDestructor DataDestructor;
bool RecursiveDelete;
// ....
~TBinTreeNode (void)
{
if (DataDestructor) DataDestructor (Data);
if (RecursiveDelete)
{
if (lnode) delete rnode;
if (rnode) delete lnode;
}
}
};


Any help appreciated, thanks :)

With best regards, Andrey Balaguta. E-mail: (e-mail address removed)
 
R

Rob Williscroft

Andrey Balaguta wrote in
Hello, Rob!
You wrote on 23 Nov 2003 19:05:19 GMT:

RW> What you've fogotten is that you can't do it (unless you have a
time machinr that is :).


I think I did that some time ago, but I will not approve that :)


RW> Perhapse you could say why you think you need them, there is most
RW> probably an alternative.


I'm doing a balanced binary search tree (template-based). A node class
has an option to destruct children recursively (i.e. node destructor
destructs node's left and right children before die itself). So there
is need to destruct data automatically, which is kind of problem,
because data may be a pointer, so it must be deleted (automatically
too). I've found only solution is to set up addition variable
(function pointer, that takes the only parameter -- data) that
destructs data (if it is null, node destructor simply ignores data).
Here is a chunk of code:

// here is a problem :)
template <class T>
typedef void (*TDataDestructor) (T data);

// ......

/* do nothing default version
template < typename T >
inline void data_destuctor( T & )
{
}

/* Overload for pointer */
template < typename T >
inline void data_destuctor( T *data )
{
delete data;
}
template <class T>
class TBinTreeNode
{
public :
//.........
T Data;
[snip]

bool RecursiveDelete;
// ....
~TBinTreeNode (void)
{

[snip]
data_destuctor( Data );
if (RecursiveDelete)
{
if (lnode) delete rnode;
if (rnode) delete lnode;
}
}
};

Rob.
 

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

Forum statistics

Threads
474,147
Messages
2,570,834
Members
47,382
Latest member
MichaleStr

Latest Threads

Top