首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >STL容器的重载运算符==

STL容器的重载运算符==
EN

Stack Overflow用户
提问于 2010-07-23 13:31:36
回答 1查看 482关注 0票数 0

我正在尝试从list<boost::any> l中删除一个类对象

代码语言:javascript
运行
复制
l.remove(class_type);

我试着把类似这样的东西写成成员函数

代码语言:javascript
运行
复制
bool operator == (const class_type &a) const //not sure about the arguments
{
   //return bool value
}

如何编写重载函数来从boost::any的std::列表中删除类的对象

EN

Stack Overflow用户

回答已采纳

发布于 2010-07-23 13:53:09

虽然您的operator==签名看起来很好,但为class_type重载它是不够的,因为boost::any并没有神奇地使用它。但是,对于删除元素,您可以将谓词传递给remove_if,例如:

代码语言:javascript
运行
复制
template<class T>
bool test_any(const boost::any& a, const T& to_test) {
    const T* t = boost::any_cast<T>(&a);
    return t && (*t == to_test);
}

std::list<boost::any> l = ...;
class_type to_test = ...;
l.remove_if(boost::bind(&test_any<class_type>, _1, to_test));
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3315582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档