C++拉姆达运算符 ==
如何在 C++ (Visual Studio 2010) 中比较两个 lambda 函数?
std::function<void ()> lambda1 = []() {};
std::function<void ()> lambda2 = []() {};
bool eq1 = (lambda1 == lambda1);
bool eq2 = (lambda1 != lambda2);
我收到一个编译错误,声称运算符 == 无法访问。
编辑:我正在尝试比较函数实例。所以 lambda1 == lambda1 应该返回 true,而 lambda1 == lambda2 应该返回 false。
How do I compare two lambda functions in C++ (Visual Studio 2010)?
std::function<void ()> lambda1 = []() {};
std::function<void ()> lambda2 = []() {};
bool eq1 = (lambda1 == lambda1);
bool eq2 = (lambda1 != lambda2);
I get a compilation error claiming that operator == is inaccessible.
EDIT: I'm trying to compare the function instances. So lambda1 == lambda1 should return true, while lambda1 == lambda2 should return false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法比较
std::function
对象,因为std::function
不可相等比较。 lambda 的闭包类型也不能进行相等比较。但是,如果您的 lambda 没有捕获任何内容,则 lambda 本身可以转换为函数指针,并且函数指针是相等可比的(但是,据我所知,完全未指定此示例中是否
are_1and2_equal
为true
或false
):Visual C++ 2010 不支持此转换。直到 Visual C++ 发布之前,该转换才添加到 C++0x 中。
You can't compare
std::function
objects becausestd::function
is not equality comparable. The closure type of the lambda is also not equality comparable.However, if your lambda does not capture anything, the lambda itself can be converted to a function pointer, and function pointers are equality comparable (however, to the best of my knowledge it's entirely unspecified whether in this example
are_1and2_equal
istrue
orfalse
):Visual C++ 2010 does not support this conversion. The conversion wasn't added to C++0x until just before Visual C++ was released.
你不能比较函数,结束。
您最多可以比较具有该概念的语言中函数的指针(这也是例如 Lisp 中的 EQ 所做的事情。对于不占用内存中相同位置的等效函数,它会失败。)
You cannot compare functions, end of.
You can at most compare pointers to functions in languages that have that concept (this is also what, for example, EQ does in Lisp. And it fails for equivalent functions that do not occupy the same place in memory.)
这是不可能的。
证明草图:如果可以计算
,那么也可以计算
并解决停止问题。
This is impossible.
Proof sketch: if it were possible to compute
then it would also be possible to compute
and solve the Halting Problem.
最简单的答案:所有模板<>类函数的运算符 ==() 是私有的。
后续问题:如果出现以下情况,您期望的是哪一个:
- 比较函数地址
- 比较两个不同的对象(类型为 std::function
- 比较两个抽象函数
编辑(近 5 年后):
我觉得很有趣的是,有没有评论的反对票。如果投反对票是因为 C++11 更改了 std::function::operator==() 的访问级别,那么我说投票者不明白时间是如何运作的。如果投反对票是因为提问者没有澄清他想象的operator==()会比较什么,也许投票者应该通过问题下的评论看到长达数小时的讨论,OP仅回答了这些讨论> 在我的答案的评论中。
Simplest answer: All of template<> class function's operator==()s are private.
Followup question: Which if the following did you expect:
- compare address of functions
- compare two distinct objects (of type std::function<void ()>
- compare two abstract functions
Edit (nearly 5 years later):
I find it amusing that there are downvotes without comments. If the downvotes are because C++11 changed the access level for std::function::operator==(), then I say the voter doesn't understand how time works. If the downvotes are because the question asker did not clarify what he imagined operator==() would compare, perhaps the voter should see the multi-hour discussion via comments immediately under the question, which the OP answered only in the comments to my answer.