重载的成员函数指针指向模板 [英] Overloaded member function pointer to template

查看:157
本文介绍了重载的成员函数指针指向模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  template< class Arg1> 
void connect(void(T :: * f)(Arg1))
{
//做一些东西
}

template< class Arg1> ;
void connect(void(T :: * f)())
{
//做一些东西
}

class GApp
{
public:
void foo(){}

void foo(double d){}
};

然后我想对GApp中的每个重载方法做如下:

  connect(& GApp :: foo); 

调用 foo() ,但是如何调用 foo(double d)?为什么以下不工作?

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

它会给我


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


在这里使用。

解决方案

编写的代码不能编译。



总而言之,您可以通过明确指定函数来调用正确的函数参数类型:

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

如果连接方法是类模板的成员,那么只需要指定类类型once:

  template< typename T> class A 
{
public:
template< class Arg1>
void connect(void(T :: * f)(Arg1))
{
//做一些东西
}

void connect (T :: * f)())
{
//做一些东西
}
};

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


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

UPDATE: / p>

响应新的代码示例,所有的信息都被传入。罕见情况是signal_void情况,因为这是信号具有模板参数,但成员函数不会。因此我们特殊情况下的例子,然后我们完成。以下现在编译:

 模板< class Arg = void> 
class signal {};
signal< double> signal_double;
信号<> signal_void;

// Arg1从信号< Arg1>然后我们在声明中使用它
//指向成员函数的指针
template< class T,class Arg1>
void connect(signal< Arg1>& sig,T& obj,void(T :: * f)(Arg1)){}

//为'void'没有任何参数
template< class T>
void connect(signal<& sig,T& obj,void(T :: * f)()){}


void bar $ b {
GApp myGApp;

//连接foo()
connect(signal_void,myGApp,& GApp :: foo); //匹配第二个重载

//连接foo(double)
connect(signal_double,myGApp,& GApp :: foo); // Matches first overload
}


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?

解决方案

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
}

这篇关于重载的成员函数指针指向模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆