views:

581

answers:

6

Hi!

I'm try to store member function pointers by templates like this. (This is a simplified version of my real code)

template<class Arg1>
void connect(void (T::*f)(Arg1)) 
{
    //Do some stuff
}

template<class Arg1>
void connect(void (T::*f)()) 
{
    //Do some stuff
}

class GApp
{
public:
    void foo() {}

    void foo(double d) {}
};

Then I want to do like following for every overloaded methods in GApp,

connect(&GApp::foo);

Calling this for foo() is ok, but how can I call this for foo(double d) ?? Why following isn't working ?

connect((&GApp::foo)(double));

It will give me "syntax error : 'double' should be preceded by ')'". I don't understand the syntax which must use here.This is may be a stupid qustion, but can anyone help me on this pls ?

Thanks

+1  A: 

You can try explicitly casting the pointer, to let it know which one to select, like this:

connect((void (GApp::*)(double))&GApp::foo);

disclaimer: haven't tested it

newacct
Thank you for the ans. Yes, it is working...
Morpheus
It can't be safe: a cast is merely a function on the GApp::foo pointer. It won't change the content of the pointer.
xtofl
I have a theory that it is safe, see my answer for details. I haven't confirmed it in the standard yet, though, because I have a strong suspicion the language in the standard will match Stroustrup's in C++PL anyway, and it's a slightly hair-splitting question.
Steve Jessop
xtofl
A: 

This is working,

    typedef void (GApp::*MemberFunctionType)(double); 
    MemberFunctionType pointer = &GApp::foo;


  connect(MemberFunctionType);

Why is that ?

EDIT

hmm.. yeah. It is same as newacct's solution. Can anyone give a solution pls?

Morpheus
My answer explains why this works, by reference to Stroustrup and an assumption that what he says about function pointers applies also to member function pointers.
Steve Jessop
+4  A: 

Your code as written doesn't compile. I've make some "assumptions" about what you wanted to do, and have changed the code.

To summarise, you can call the correct function by explicitly specifying the function parameter type:

connect<double> (&GApp::foo);

If the connect methods are members of a class template, then it is only necessary to specify the class type once:

template <typename T> class A
{
public:
  template<class Arg1>
  void connect(void (T::*f)(Arg1)) 
  {
    //Do some stuff
  }

  void connect(void (T::*f)()) 
  {
    //Do some stuff
  }
};

class GApp
{
public:
    void foo() {}
    void foo(double d) {}
};


int main ()
{
  A<GApp> a;
  a.connect (&GApp::foo);            // foo ()
  a.connect<double> (&GApp::foo);    // foo (double)
}

UPDATE:

In response to the new code sample, all the information is being passed in. The "rare" case is the 'signal_void' case as this is where the signal has a template argument, but the member function doesn't. Therefore we special case that example and then we're done. The following now compiles:

template <class Arg = void>
class signal {};
signal<double> signal_double;
signal<> signal_void;

// Arg1 is deduced from signal<Arg1> and then we use it in the declaration
// of the pointer to member function
template<class T, class Arg1>
void connect ( signal<Arg1>& sig, T& obj, void (T::*f)(Arg1) ) {}

// Add special case for 'void' without any arguments
template<class T>
void connect (signal<> & sig, T& obj, void (T::*f)()) {}


void bar ()
{
  GApp myGApp;

  //Connecting foo()
  connect(signal_void, myGApp, &GApp::foo); // Matches second overload

  //Connecting foo(double)
  connect(signal_double, myGApp, &GApp::foo); // Matches first overload
}
Richard Corden
Hey! Thanks for the effort. Sorry, I forgot to tell that my code isn't complete. It was because I removed Signal template class to simplify the code. BTW, yes, your method is working well. I'll post my original code, can you look at it then ? Thanks
Morpheus
Wohooooo.... yeah. You show me the path. Now all are working correctly. Thank you very much.
Morpheus
A: 

Using boost::function library...

#include <boost/function.hpp>

template<class Arg1>
void connect(boost::function1<void, Arg1*> fn) 
{
    //Do some stuff
}

template<class Arg1>
void connect(boost::function2<void, Arg1*, double> fn) 
{
    //Do some stuff
}

class GApp
{
public:
    void foo() {}

    void foo(double d) {}
};


int main()
{
    boost::function1<void,GApp*> f1 = (void (GApp::*)(void)) &GApp::foo;
    boost::function2<void,GApp*,double> f2 = (void (GApp::*)(double)) &GApp:foo;
    connect(f1);
    connect(f2);
    return 0;
}
Anshul
Hey! Thanks for the ans. Isn't your (void (GApp::*)(double))
Morpheus
xtofl has updated comment to say that it seems to be safe. Its preferable to use function objects rather than function pointers, that's all.
Anshul
A: 

Hi!

My Original code is like this,

connectors....

template<class T, class Arg1>
void connect(signal<Arg1>& sig,T& obj, void (T::*f)()) 
{
//  sig.insert(new GFunction<T, Arg1>(&obj,f));
}

template<class T, class Arg1
void connect(signal<Arg1>& sig,T& obj, void (T::*f)(Arg1)) 
{
//  sig.insert(new GFunction<T, Arg1>(&obj,f));
}

Signals...

signal<double> signal_double;
signal<> signal_void;

Application...

class GApp
{
public:
    void foo() {}

    void foo(double d) {}
};

Finaly, connecting...

//Connecting foo()
        connect(signal_void, myGApp, &GApp::foo()); //Ok

//Connecting foo(double)
        connect(signal_double, myGApp, &GApp::foo()); // but ERROR!

There is template classs for signals (which are not mention here). I hope now it is more clear the situation. (or is it same as previouse ?). That second connection will WORK if there isn't foo() (only foo(double)). That is the thing my code hurt me. :(

Morpheus
+5  A: 

The C++ Programming Language, 3E, Section 7.7, p159:

You can take the address of an overloaded function by assigning to or initializing a pointer to function. In that case, the type of the target is used to select from the set of overloaded functions. For example:

void f(int);
int f(char);

void (*pf1)(int) = &f;  // void f(int);
int (*pf2)(char) = &f;  // int f(char);
void (*pf3)(char) = &f; // error: no void f(char)

As far as I know (haven't checked), the same applies to member functions. So the solution is probably to split across two lines:

connect((&GApp::foo)(double));

becomes:

void (GApp::*tmp)(double) = &GApp::foo;
connect(tmp);

Never call variables tmp ;-)

I would guess that newacct's cast is safe too, for exactly the same reason. Casting to void (GApp::*)(double) is defined to be the same as initializing a temporary of type void (GApp::*)(double). Since the expression used to initialize it is &GApp::foo, I would expect the same magic to apply to the cast as applies to any other initialization with an overloaded function. Stroustrup doesn't say "initializing a pointer-to-function variable", he says "initializing a pointer-to-function". So that should include temporaries.

So if you prefer a one-liner:

connect((void (GApp::*)(double))(&GApp::foo));

However, I'm assuming that the standard has the same idea of consistency as I do, and I haven't checked.

Steve Jessop
thANKS! So that's why the cast will work!
xtofl
And they say C++ is hard to understand! ;-)
Steve Jessop