成员函数可以通过哪些方式进行相互比较?
我想知道是否可以将 2 个成员函数与“<”进行比较操作员。我可以做“==”,但在下面的情况下我不能使用它。我尝试将它们转换为 void* 但这也不起作用。
template <class Receiver, class Sender>
class CallBack2 : public ICallBack2 {
protected:
Receiver* receiver;
void(Receiver::*function)(Sender*);
Sender* sender;
public:
CallBack2(Receiver* _receiver, void(Receiver::*_function)(Sender*), Sender* _sender) : receiver(_receiver), function(_function), sender(_sender) {};
virtual ~CallBack2() {};
virtual void callBack() {
(receiver->*function)(sender);
}
virtual bool operator<(const ICallBack2* _other) const {
CallBack2<Receiver, Sender>* other = (CallBack2<Receiver, Sender>*)_other;
if (receiver < other->receiver) {
return true;
} else if (receiver == other->receiver && function < other->function) {
return true; // this line gives the error
}
return false;
}
};
有什么想法吗?
I would like to know if I can compare 2 member functions with the "<" operator. I can do "==" but I can't use it in the case below. I tried casting them to void* but that won't work either.
template <class Receiver, class Sender>
class CallBack2 : public ICallBack2 {
protected:
Receiver* receiver;
void(Receiver::*function)(Sender*);
Sender* sender;
public:
CallBack2(Receiver* _receiver, void(Receiver::*_function)(Sender*), Sender* _sender) : receiver(_receiver), function(_function), sender(_sender) {};
virtual ~CallBack2() {};
virtual void callBack() {
(receiver->*function)(sender);
}
virtual bool operator<(const ICallBack2* _other) const {
CallBack2<Receiver, Sender>* other = (CallBack2<Receiver, Sender>*)_other;
if (receiver < other->receiver) {
return true;
} else if (receiver == other->receiver && function < other->function) {
return true; // this line gives the error
}
return false;
}
};
Any ideas please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C++03 § 5.9,它涵盖了内置 <、>、<= 和 >= 的语义,没有提到指向成员和状态的指针:
根据§ 8.3.3, 3
因此,我们可以得出结论,应用于指向成员(无论是函数还是字段)的指针的关系运算符的结果是未指定的。
请注意,“未指定的行为”与“未定义的行为”不同,但仍然意味着您无法有效地应用运算符,因为不同的实现可能会产生不同的结果。 “未指定”基本上意味着实现可以定义行为。
C++03 § 5.9, which covers the semantics of the built-in <, >, <= and >=, doesn't mention pointers to members and states:
According to § 8.3.3, 3
As a result, we can conclude the result of relational operators applied to pointers to members (whether functions or fields) is unspecified.
Note that "unspecified behavior" is different from "undefined behavior", but still means you can't usefully apply the operators as different implementations may have different results. "Unspecified" basically means the implementation gets to define the behavior.
如果您只想任意将它们排序为集合/映射中的键,那么您可以
reinterpret_cast
它们。您可能需要像exact_int::type
这样的模板类,因为 指向成员函数的指针可以有有趣的大小。If you just want to arbitrarily order them to be keys in a set/map, then you can
reinterpret_cast
them. You may need a template class likeexact_int<sizeof(void (Foo::*bar)())>::type
because pointers to member functions can have funny sizes.5.9.7(关系运算符):“其他指针比较未指定”。
由于 5.9 不清楚(它处理函数,但没有明确的成员函数),快速浏览一下 5.10(相等比较)可以清楚地将函数与成员函数区分开来:
所以可以使用运算符,
==
和!=
的含义是指定的,但是<
、>的含义;
、<=
和>=
未指定。特别是,没有任何东西强制传递性,因此不清楚将它们放入集合中是否可以。
5.9.7 (relational operators): "Other pointer comparisons are unspecified".
Since 5.9 is unclear (it deals with functions, but not explicitly member functions), a quick look at 5.10 (equality comparison) clearly separates functions from member functions:
So you can use the operators, the meaning of
==
and!=
is specified, but the meaning of<
,>
,<=
and>=
is unspecified.In particular, nothing enforces transitivity, so it is not clear whether putting them in a set is ok or not.
虽然描述有点长,但是有一个假人怎么样?
变量并比较其指针,如下所示?
这是对 ideone 的测试。
Though the description gets a little lengthy, how about having a dummy
variable and comparing its pointer like the following?
Here is a test on ideone.
您可以执行类似 Ise 的想法,仅将其包含在 Callback2 类中,这样您就不需要更改使用该类的任何内容。
You could do something like Ise's idea, only keep it contained to Callback2 class so that you don't need to change anything that uses the class.
获取成员函数的地址会产生无法存储在变量中的常量表达式。它只能用于与另一个表达式进行比较是否相等,该表达式表示采用相同参数集、具有相同返回类型和相同类型的 this 指针的函数的地址。
Taking the address of a member function results in a constant expression that cannot be stored in a variable. It can only be used to compare for equality to another expression that represenst the address of a function taking the same set of parameters, with the same return type and the same type of this pointer.
比较两个函数指针是没有意义的。您可以比较的实际上是这些函数的返回值:
但在您的情况下,您将函数声明为:
所以,在我看来。比较功能是没有用的。更改函数的签名以返回某些内容或更好地描述您的业务场景,以便我们更好地了解您想要什么。
It makes no sense to compare two function pointers. What you could compare is actually the return value of those functions:
but in your case you declare the function as:
so, in my opinion. Comparing the functions is useless. Either change the function's signature to return something or better describe your business scenario so we can better understand what you want.