typedef for template function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrey Balaguta

    typedef for template function

    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 (*TDataDestruct or) (T Data)" and "template <class T> typedef
    void (*TDataDestruct or) (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: uj2@mail.ru


  • Rob Williscroft

    #2
    Re: typedef for template function

    Andrey Balaguta wrote in news:3fc0ff0e$1 @news.itcom.net .ua:
    [color=blue]
    > 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 (*TDataDestruct or) (T Data)" and
    > "template <class T> typedef void (*TDataDestruct or) (T Data)", both
    > don't work with error "template must be classes or functions"
    > (bcc5.5). Point me to right answer :) Thanks in advance
    >[/color]

    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.
    --

    Comment

    • Andrey Balaguta

      #3
      Re: typedef for template function

      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 (*TDataDestruct or) (T data);

      // ......

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


      Any help appreciated, thanks :)

      With best regards, Andrey Balaguta. E-mail: uj2@mail.ru


      Comment

      • Rob Williscroft

        #4
        Re: typedef for template function

        Andrey Balaguta wrote in news:3fc10c61$1 @news.itcom.net .ua:
        [color=blue]
        > 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 (*TDataDestruct or) (T data);
        >
        > // ......
        >[/color]

        /* 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;
        }
        [color=blue]
        > template <class T>
        > class TBinTreeNode
        > {
        > public :
        > //.........
        > T Data;[/color]

        [snip]
        [color=blue]
        > bool RecursiveDelete ;
        > // ....
        > ~TBinTreeNode (void)
        > {[/color]

        [snip]
        data_destuctor( Data );
        [color=blue]
        > if (RecursiveDelet e)
        > {
        > if (lnode) delete rnode;
        > if (rnode) delete lnode;
        > }
        > }
        > };
        >[/color]

        Rob.
        --

        Comment

        Working...