重载指向模板的成员函数指针
我试图通过这样的模板存储成员函数指针:(这是我的真实代码的简化版本)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您编写的代码无法编译。 我对你想要做什么做了一些“假设”,并更改了代码。
总而言之,您可以通过显式指定函数参数类型来调用正确的函数:
如果连接方法是类模板的成员,则只需指定类类型一次:
更新:
在响应新的代码示例,所有信息都被传入。“罕见”的情况是“signal_void”情况,因为这是信号具有模板参数,但成员函数没有的情况。 因此,我们对这个例子进行特殊处理,然后就完成了。 现在可以编译以下内容:
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:
If the connect methods are members of a class template, then it is only necessary to specify the class type once:
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:
C++ 编程语言,3E,第 7.7 节,第 159 页:
据我所知(尚未检查),这同样适用于成员函数。 因此,解决方案可能分为两行:
变成:
永远不要调用变量
tmp
;-)我猜想 newacct 的强制转换也是安全的,原因完全相同。 转换为
void (GApp::*)(double)
的定义与初始化void (GApp::*)(double)
类型的临时变量相同。 由于用于初始化它的表达式是&GApp::foo
,我希望同样的魔法也适用于强制转换,就像适用于任何其他具有重载函数的初始化一样。 Stroustrup 没有说“初始化函数指针变量”,他说的是“初始化函数指针”。 所以这应该包括临时工。因此,如果您更喜欢单行本:
但是,我假设该标准与我具有相同的一致性理念,并且我还没有检查过。
The C++ Programming Language, 3E, Section 7.7, p159:
As far as I know (haven't checked), the same applies to member functions. So the solution is probably to split across two lines:
becomes:
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 typevoid (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:
However, I'm assuming that the standard has the same idea of consistency as I do, and I haven't checked.
您可以尝试显式转换指针,让它知道要选择哪一个,如下所示:
免责声明:尚未测试它
You can try explicitly casting the pointer, to let it know which one to select, like this:
disclaimer: haven't tested it
这是有效的,
为什么呢?
编辑
嗯..是的。 与newacct的解决方案相同。 任何人都可以提供解决方案吗?
This is working,
Why is that ?
EDIT
hmm.. yeah. It is same as newacct's solution. Can anyone give a solution pls?
使用 boost:: 函数库...
Using boost::function library...
我的原始代码是这样的,
连接器...
信号...
应用程序...
最后,连接...
有信号的模板类(这里没有提到)。 我希望现在情况更加清楚了。 (或者与之前的相同?)。 如果没有 foo() (只有 foo(double)),第二个连接将起作用。 这就是我的代码伤害我的地方。 :(
My Original code is like this,
connectors....
Signals...
Application...
Finaly, connecting...
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. :(