Template methods with function pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • canderse@gmail.com

    Template methods with function pointers

    I am beginning to use templates alot (in visual studio 2008) but I
    writing this simple template
    that has a method which takes a function pointer as an argument and i
    cant figure out why the will not compile

    LONG __cdecl FilterListProc( LPVOID Item1,LPVOID Item2,LPVOID pParam =
    NULL);

    template<class TItemList,class TItemData>
    class CFiltredListImp l
    {
    private:
    class vaiables anf methods
    ...
    public:
    BOOL Filter(LONG (__cdecl *FilterProc)(LP VOID,LPVOID,LPV OID),LPVOID
    lpPram = NULL)
    {
    return TRUE;
    };

    };

    idearly it would like the function to look like this

    LONG __cdecl FilterListProc( LPVOID Item1,LPVOID Item2,LPVOID pParam =
    NULL);

    template<class TItemList,class TItemData>
    class CFiltredListImp l
    {
    private:
    class vaiables anf methods
    ...
    public:
    BOOL Filter(LONG (__cdecl *FilterProc)(LP VOID,LPVOID,LPV OID) =
    FilterListProc ,LPVOID lpPram = NULL)
    {
    return TRUE;
    };

    };

    or even better it would be better if I could write it like this

    template<TItemD ata>
    LONG __cdecl FilterListProc( TItemData & Item1,TItemData & Item2,LPVOID
    pParam = NULL);

    template<class TItemList,class TItemData>
    class CFiltredListImp l
    {
    private:
    class vaiables anf methods
    ...
    public:
    BOOL Filter(LONG (__cdecl *FilterProc)(TI temData &,TItemData,LPV OID)
    = FilterListProc ,LPVOID lpPram = NULL)
    {
    return TRUE;
    };

    };


    My first question is why will the first version compile and my second
    is the other two implementation feasible

    Your help would be apreciated

    Thanks Christian Arild Stær Andersen
  • canderse@gmail.com

    #2
    Re: Template methods with function pointers

    I did get it to compile with this

    template<TItemD ata>
    LONG __cdecl FilterListProc( TItemData & Item1,TItemData & Item2,LPVOID
    pParam = NULL);

    template<class TItemList,class TItemData>
    class CFiltredListImp l
    {
    private:
    class vaiables anf methods
    ...
    public:
    BOOL Filter(LONG (__cdecl *FilterProc)(TI temData &,TItemData
    & ,LPVOID),LPVOID lpPram = NULL)
    {
    return TRUE;
    };

    };

    Comment

    • dascandy@gmail.com

      #3
      Re: Template methods with function pointers

      On Oct 31, 6:24 am, cande...@gmail. com wrote:
      I did get it to compile with this
      >
      template<TItemD ata>
      LONG __cdecl FilterListProc( TItemData & Item1,TItemData & Item2,LPVOID
      pParam = NULL);
      >
      template<class TItemList,class TItemData>
      class CFiltredListImp l
      {
              private:
              class vaiables anf methods
              ...
              public:
              BOOL Filter(LONG (__cdecl *FilterProc)(TI temData &,TItemData
      & ,LPVOID),LPVOID lpPram = NULL)
              {
                      return TRUE;
              };
      >
      };
      >
      >
      The second misses an ampersand in front of the function name, I'm not
      sure if that's all.

      The third misses both the ampersand and the template instantiation
      parameters, which means that you can't pass the plain template - you
      have to pass it as an instantiated function:

      BOOL Filter(LONG (__cdecl *FilterProc)(TI temData &,TItemData
      & ,LPVOID) = &FilterListProc <TItemData>,LPV OID lpPram = NULL)

      which should work.

      Good luck.

      Comment

      Working...