重载指向模板的成员函数指针

发布于 2024-07-25 23:19:33 字数 688 浏览 1 评论 0原文

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

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()< 调用此方法/code> 可以,但是我如何为 foo(double d) 调用它? 为什么以下不起作用?

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

它会给我

语法错误:“double”前面应带有“)”

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

薄荷港 2024-08-01 23:19:33

您编写的代码无法编译。 我对你想要做什么做了一些“假设”,并更改了代码。

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

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

如果连接方法是类模板的成员,则只需指定类类型一次:

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

更新:

在响应新的代码示例,所有信息都被传入。“罕见”的情况是“signal_void”情况,因为这是信号具有模板参数,但成员函数没有的情况。 因此,我们对这个例子进行特殊处理,然后就完成了。 现在可以编译以下内容:

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
}

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
}
歌枕肩 2024-08-01 23:19:33

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

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

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 的强制转换也是安全的,原因完全相同。 转换为 void (GApp::*)(double) 的定义与初始化 void (GApp::*)(double) 类型的临时变量相同。 由于用于初始化它的表达式是 &GApp::foo,我希望同样的魔法也适用于强制转换,就像适用于任何其他具有重载函数的初始化一样。 Stroustrup 没有说“初始化函数指针变量”,他说的是“初始化函数指针”。 所以这应该包括临时工。

因此,如果您更喜欢单行本:

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

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

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.

楠木可依 2024-08-01 23:19:33

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

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

免责声明:尚未测试它

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

蓝眸 2024-08-01 23:19:33

这是有效的,

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


  connect(MemberFunctionType);

为什么呢?

编辑

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

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?

听风念你 2024-08-01 23:19:33

使用 boost:: 函数库...

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

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;
}
冬天的雪花 2024-08-01 23:19:33

我的原始代码是这样的,

连接器...

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)),第二个连接将起作用。 这就是我的代码伤害我的地方。 :(

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. :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文