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

typedef for template function

P: n/a
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: uj*@mail.ru
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andrey Balaguta wrote in news:3f********@news.itcom.net.ua:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
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: uj*@mail.ru
Jul 22 '05 #3

P: n/a
Andrey Balaguta wrote in news:3f********@news.itcom.net.ua:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.