UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » how to use typedef'd template member function pointer?

#1 2008-07-03 03:34 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how to use typedef'd template member function pointer?

hi all
in the following code i try to use a typedef'd template member function pointer as a function argument.
The problem is located at definition of the template function callit()
Anyone knows why i can't use
test<TClass>::testfunc
within a template function?

// g++ -o templatepointer templatepointer.cpp
// templatepointer.cpp:22: error: ‘class test<TClass>::testfunc’ is not a type
// templatepointer.cpp: In function ‘int main(int, char**)’:
// templatepointer.cpp:35: error: no matching function for call to ‘callit(A*, void (A::*)(int))’
//
#include <stdio.h>

template<class TClass>
struct test{
    public:
      typedef void(TClass::*testfunc)(int);
};

class A{
   public:
      A(){}
      ~A(){}
      void print(int a){ printf("%d\n",a); }
};

template<class TClass>
void callit(TClass *instance,test<TClass>::testfunc f){     // typedef member function pointer would be better to read (for me)
    (*instance.*f)(10);
}
/*
template<class TClass>
void callit(TClass *instance,void(TClass::*f)(int)){    // << this works
    (*instance.*f)(11);
}
*/
int main(int argc,char **argv){
    test<A>::testfunc somepointer;  // this works

    A inst;
    callit<A>(&inst,&A::Print);
    return 0;
}

Offline

#2 2008-07-03 04:43 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how to use typedef'd template member function pointer?

I'm not very good at C++, but I'm pretty sure you need to move the typedef out of that struct and add a struct member. Something like the following should be
slightly more in the right direction:

typedef void(TClass::*testfunc)(int);

struct test{
    public:
      testfunc bla;
};


Or just remove the "typedef" part and leave the rest.

Typedefs are just syntactic sugar, they don't really do anything, you can't use
them for declarations.

But using functions pointers the way you are is a bit messy and shouldn't
be necessary with proper classes and inheritance usage.

Offline

#3 2008-07-04 05:04 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: how to use typedef'd template member function pointer?

Offline

#4 2008-07-04 08:29 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how to use typedef'd template member function pointer?

Offline

#5 2008-07-05 10:41 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: how to use typedef'd template member function pointer?

class Foo
{
  public:

    typedef void ( Foo::*FooIntFunc ) ( int );
}

// All plug-ins extend Foo so we can do our casting & 'fake' validation

class Bar : public Foo
{
  public:

    Bar( ) { }

    void print_0( int x ) { printf( "Bar 0\n" ); }
    void print_1( int x ) { printf( "Bar 1\n" ); }
};

void callit( Foo & foo, Foo::FooIntFunc func, int x )
{
  ( foo.*func ) ( x );
}

int main( int c, char ** v )
{
  Bar bar;

  callit( bar, static_cast< Foo::FooIntFunc > ( & A::Print_0 ), 0 );
  callit( bar, static_cast< Foo::FooIntFunc > ( & A::Print_1 ), 1 );
}

"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2008-07-06 09:11 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how to use typedef'd template member function pointer?

i think the interface approach isnt what i need for my app
generally a plugin itself isnt defined in what it does or can do
the plugins only register functions and other plugins can use them
so my core app does only handle some kind of function mapping between them
which means i dont know if the final application will use a GUI or networking or is just some kind of cronjob,...
but i keep it in mind

for the static cast
i think i can and will use it, im not at the office currently but im sure it will work
[email protected]

Offline

#7 2008-07-07 01:27 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: how to use typedef'd template member function pointer?

As for me i would prefer boost::function when i need to work with functors. But maybe you don't need this.Just want to notice that it's interesting,useful and easy to use class.

Offline

  • Index
  • » C++
  • » how to use typedef'd template member function pointer?

Board footer

Powered by FluxBB