C++拉姆达运算符 ==

发布于 2024-09-28 15:11:30 字数 361 浏览 2 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨后咖啡店 2024-10-05 15:11:30

您无法比较 std::function 对象,因为 std::function 不可相等比较。 lambda 的闭包类型也不能进行相等比较。

但是,如果您的 lambda 没有捕获任何内容,则 lambda 本身可以转换为函数指针,并且函数指针是相等可比的(但是,据我所知,完全未指定此示例中是否 are_1and2_equaltruefalse):

void(*lambda1)() = []() { };
void(*lambda2)() = []() { };
bool are_1and1_equal = (lambda1 == lambda1); // will be true
bool are_1and2_equal = (lambda1 == lambda2); // may be true?

Visual C++ 2010 不支持此转换。直到 Visual C++ 发布之前,该转换才添加到 C++0x 中。

You can't compare std::function objects because std::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 is true or false):

void(*lambda1)() = []() { };
void(*lambda2)() = []() { };
bool are_1and1_equal = (lambda1 == lambda1); // will be true
bool are_1and2_equal = (lambda1 == lambda2); // may be true?

Visual C++ 2010 does not support this conversion. The conversion wasn't added to C++0x until just before Visual C++ was released.

金橙橙 2024-10-05 15:11:30

你不能比较函数,结束。

您最多可以比较具有该概念的语言中函数的指针(这也是例如 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.)

策马西风 2024-10-05 15:11:30

这是不可能的。

证明草图:如果可以计算

f1 == f2

,那么也可以计算

f == infiniteLoop

并解决停止问题。

This is impossible.

Proof sketch: if it were possible to compute

f1 == f2

then it would also be possible to compute

f == infiniteLoop

and solve the Halting Problem.

完美的未来在梦里 2024-10-05 15:11:30

最简单的答案:所有模板<>类函数的运算符 ==() 是私有的。

后续问题:如果出现以下情况,您期望的是哪一个:
- 比较函数地址
- 比较两个不同的对象(类型为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文