免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2173 | 回复: 3

[C++] [请教]这个仿函数为什么必须加const [复制链接]

论坛徽章:
0
发表于 2013-04-26 22:51 |显示全部楼层
本帖最后由 kingoftime3 于 2013-04-26 22:53 编辑
  1. #include <list>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <functional>
  5. #include <string>

  6. using namespace std;

  7. template<class T>
  8. struct IsSame : binary_function<pair<string,string>, T, bool>
  9. {
  10.         bool operator()(const pair<string,string> &p, const T &s) const
  11.         {
  12.                 return p.first==s;
  13.         }
  14. };

  15. static void print(const pair<string, string> &p)
  16. {
  17.         cout << p.first << "\t" << p.second << endl;
  18. }

  19. int main()
  20. {
  21.         list<pair<string,string> > mlist;
  22.         mlist.push_back(pair<string,string>("aaa", "aaa"));
  23.         mlist.push_back(pair<string,string>("aaa1", "aaa"));
  24.         mlist.push_back(pair<string,string>("aaa2", "aaa"));
  25.         mlist.push_back(pair<string,string>("aaa3", "aaa"));
  26.         mlist.remove_if(binder2nd<IsSame<string> >(IsSame<string>(), "aaa"));
  27.         for_each(mlist.begin(), mlist.end(), print);
  28.         system("pause");
  29.         return 0;

  30. }
复制代码
如果
  1. bool operator()(const pair<string,string> &p, const T &s) const
复制代码
写成
  1. bool operator()(const pair<string,string> &p, const T &s)
复制代码
就会报错
  1. 1>d:\program files\microsoft visual studio 11.0\vc\include\xfunctional(329): error C3848: 具有类型“const IsSame<T>”的表达式会丢失一些 const-volatile 限定符以调用“bool IsSame<T>::operator ()(const std::pair<_Ty1,_Ty2> &,const T &)”
  2. 1>          with
  3. 1>          [
  4. 1>              T=std::string
  5. 1>          ]
  6. 1>          d:\program files\microsoft visual studio 11.0\vc\include\xfunctional(328): 编译类 模板 成员函数“bool std::binder2nd<_Fn2>::operator ()(std::pair<_Ty1,_Ty2> &) const”时
  7. 1>          with
  8. 1>          [
  9. 1>              _Fn2=IsSame<std::string>,
  10. 1>              _Ty1=std::string,
  11. 1>              _Ty2=std::string
  12. 1>          ]
  13. 1>          d:\program files\microsoft visual studio 11.0\vc\include\list(1615): 参见对正在编译的函数 模板 实例化“bool std::binder2nd<_Fn2>::operator ()(std::pair<_Ty1,_Ty2> &) const”的引用
  14. 1>          with
  15. 1>          [
  16. 1>              _Fn2=IsSame<std::string>,
  17. 1>              _Ty1=std::string,
  18. 1>              _Ty2=std::string
  19. 1>          ]
  20. 1>          c:\users\chenxie\documents\visual studio 2012\projects\simpletest\simpletest\simpletest.cpp(30): 参见对正在编译的类 模板 实例化“std::binder2nd<_Fn2>”的引用
  21. 1>          with
  22. 1>          [
  23. 1>              _Fn2=IsSame<std::string>
  24. 1>          ]
复制代码

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
发表于 2013-04-27 12:52 |显示全部楼层
因为 binder2nd 的 operator() 后面有个 const

另外 binary_function、binder2nd 都是要废弃的东西,没必要研究了
system("pause") 嘛,不想评价

论坛徽章:
0
发表于 2013-04-27 14:10 |显示全部楼层
回复 2# bruceteen


    binder2nd如果废弃了有没有什么替代的接口可以实现同样的功能呢?

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
发表于 2013-04-27 14:24 |显示全部楼层
回复 3# kingoftime3
lambda表达式 和 std::function
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP