比较std :: functions的相等性? [英] Comparing std::functions for equality?

查看:884
本文介绍了比较std :: functions的相等性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较两个C ++ 11 std :: function s与 operator == ,并返回 true 如果函数都指向相同的函数指针?

How can I compare two C++11 std::functions with operator==, and return true if both of said functions refer to the same function pointer?

推荐答案

operator == std :: function 比较一个 std :: function 与一个空指针,就我可以告诉标准不提供任何细节为什么。

operator== for std::function compares a std::function with a null pointer, as far as I can tell the standard does not provide any details as to why.

虽然,这个提升常见问题项目,为什么我不能比较boost :: function对象与operator ==或operator!=?提供了一个基本原理,据我所知,应该适用于std::function 。引用常见问题解答:

Although, this boost FAQ entry, Why can't I compare boost::function objects with operator== or operator!=? provides a rationale and as far as I can tell should be applicable to std::function as well. Quoting the FAQ:


boost :: function对象之间的比较不能实现很好,因此不会实现。 [...]

Comparison between boost::function objects cannot be implemented "well", and therefore will not be implemented. [...]

然后概述类似于Preet的请求解决方案,并继续说:

it then outlines requested solutions similar to Preet's and goes on to say:


当f和g存储的函数对象的类型没有运算符时,会出现问题== [...]

The problem occurs when the type of the function objects stored by both f and g doesn't have an operator==[...]

并解释为什么必须在赋值运算符或构造函数中处理,然后继续说:

and explains why this has to has to be dealt with in either the assignment operator or constructor and then goes on to say:


所有这些问题都转化为boost :: function构造函数或赋值运算符的失败,即使用户从不调用operator ==。我们不能向用户这样做。

All of these problems translate into failures in the boost::function constructors or assignment operator, even if the user never invokes operator==. We can't do that to users.

更新

找到tr1的目标方面找到了标准原理:: function object ,它很漂亮,但与boost常见问题一致,并说:

Found a standards rationale in Accessing the target of a tr1::function object, which is pretty old but is consistent with the boost FAQ and says:


operator == is notinplementable对于tr1 :: function在C ++语言中,因为我们没有一个可靠的方法来检测给定类型T是否为Equality Comparable没有用户帮助。

operator== is unimplementable for tr1::function within the C++ language, because we do not have a reliable way to detect if a given type T is Equality Comparable without user assistance.

这篇关于比较std :: functions的相等性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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