Overloaded member function pointer to template
c++, pointer-to-member, templates
Solution
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
}
Problem
I'm trying 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 the 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 isn't the following working? ``` connect((&GApp::foo)(double)); ``` It will give me syntax error : 'double' should be preceded by ')' I don't understand the syntax which must be used here. This may be a stupid qustion, but can any one help me on this?