6

我正在尝试通过这样的模板存储成员函数指针:(这是我真实代码的简化版本)

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) {}
};

然后我想对 GApp 中的每个重载方法执行以下操作:

connect(&GApp::foo); 

调用这个foo()是可以的,但我怎么能调用这个foo(double d)呢?为什么以下不起作用?

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

它会给我

语法错误:'double' 应该以 ')' 开头

我不明白这里必须使用的语法。这可能是一个愚蠢的问题,但是任何人都可以帮助我吗?

4

6 回答 6

6

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
}
于 2009-07-08T10:12:22.303 回答
5

C++ 编程语言,3E,第 7.7 节,p159:

您可以通过分配或初始化指向函数的指针来获取重载函数的地址。在这种情况下,目标的类型用于从重载函数集中进行选择。例如:

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)

据我所知(尚未检查),这同样适用于成员函数。所以解决方案可能是分成两行:

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

变成:

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

永远不要调用变量tmp;-)

我猜 newacct 的演员也是安全的,原因完全相同。Casting tovoid (GApp::*)(double)被定义为与初始化临时类型的类型相同void (GApp::*)(double)。由于用于初始化它的表达式是&GApp::foo,因此我希望将同样的魔法应用于强制转换,就像应用于任何其他具有重载函数的初始化一样。Stroustrup 没有说“初始化一个指向函数的变量”,他说的是“初始化一个指向函数的指针”。所以这应该包括临时工。

因此,如果您更喜欢单线:

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

但是,我假设该标准与我具有相同的一致性概念,并且我没有检查过。

于 2009-07-08T11:13:28.857 回答
1

您可以尝试显式转换指针,让它知道要选择哪一个,如下所示:

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

免责声明:尚未测试

于 2009-07-08T09:00:27.160 回答
0

这是工作,

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


  connect(MemberFunctionType);

这是为什么 ?

编辑

嗯..是的。与newacct 的解决方案相同。任何人都可以提供解决方案吗?

于 2009-07-08T09:06:32.780 回答
0

使用 boost::function 库...

#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;
}
于 2009-07-08T10:36:18.463 回答
0

我的原始代码是这样的,

连接器……

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));
}

信号...

signal<double> signal_double;
signal<> signal_void;

应用...

class GApp
{
public:
    void foo() {}

    void foo(double d) {}
};

最后,连接...

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

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

有信号的模板类(这里没有提到)。我希望现在情况更清楚了。(或者它与以前的相同?)。如果没有 foo()(只有 foo(double)),则第二个连接将起作用。这就是我的代码伤害我的地方。:(

于 2009-07-08T10:46:20.597 回答