gpt4 book ai didi

c++ - 比较 std::functions 是否相等?

转载 作者:太空宇宙 更新时间:2023-11-04 13:40:18 31 4
gpt4 key购买 nike

我如何比较两个 C++11 std::functionoperator==,如果两者都说则返回 true 函数引用同一个函数指针?

最佳答案

operator== for std::functionstd::function 与空指针进行比较,据我所知标准没有提供任何详细信息。

尽管如此,此提升常见问题解答条目,Why can't I compare boost::function objects with operator== or operator!=?提供了一个基本原理,据我所知应该适用于 std::function以及。引用常见问题解答:

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

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

The problem occurs when the type of the function objects stored by both f and g doesn't have an 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.

更新

Accessing the target of a tr1::function object 中找到标准原理,这是相当古老的,但与提升常见问题解答一致,并说:

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.

关于c++ - 比较 std::functions 是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27949100/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com