gpt4 book ai didi

带有 std::function 的 C++ std::list:调用 remove() 不会构建

转载 作者:行者123 更新时间:2023-12-04 01:02:59 24 4
gpt4 key购买 nike

谁能解释一下为什么这段代码无法构建:

std::function<void(int)> intcb = [](int a)
{
std::cout << "callback: " << a << std::endl;
};

std::list<std::function<void(int)>> list;
list.push_back(intcb);
list.remove(intcb);

构建错误位于删除函数中:

no match for 'operator==' (operand types are 'std::function<void(int)>' and 'const value_type' {aka 'const std::function<void(int)>'})

谢谢你的帮助

最佳答案

好吧,您想创建一个函数列表,然后启用“删除”功能,但是要进行删除操作,列表需要能够比较其类型的实例,因为它不依赖于对对象的引用。

因此您需要以某种方式为指定的函数类型定义operator==

关于带有 std::function 的 C++ std::list:调用 remove() 不会构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67582861/

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