为什么bind2nd(mem_fun_ref(&CLASS::Func),Para);CLASS::Func的参数不能是pass-by-(const)reference

fangrk 2003-09-09 09:25:01
//为什么bind2nd(mem_fun_ref(&CLASS::Func),Para);CLASS::Func的参数不能是pass-by-(const)reference
#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;

class Test
{
public:
Test(int i){weight=i;}
bool Heavier(const int& k) const//pass-by-(const)reference
{return weight>k;}
int weight;
};

int main()
{
list<Test> TL;
for(int i=50;i<60;++i) TL.push_back(Test(i));
int w=55;
list<Test>::const_iterator F=find_if(TL.begin(),TL.end(),bind2nd(mem_fun_ref(&Test::Heavier),w));
if(F!=TL.end()) cout<<F->weight;
}
/*
BCB6和DEV-C++4都无法编译
如果把T::Heavier的参数从pass-by-(const)reference修改为pass-by-value就正确
bool Heavier(int k) const is OK!
很不解为什么pass-by-(const)reference就无法通过编译
如果无法解决,我只能pass-by-pointer了
*/
...全文
74 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangrk 2003-09-10
  • 打赏
  • 举报
回复
嗯,我看书不仔细
//STL源码剖析
template<class Operation>
class bind2nd
{
public:
...
binder2nd(const Operation&,typename Operation::second_arguement_type&);
};
在我的例子中second_argument_type就是const int&
上面就变成了const int& &了
jyfcsdn 2003-09-10
  • 打赏
  • 举报
回复
学习
确实如上面的高手所说 (const int& &)这种类型编译通不过
kweio 2003-09-09
  • 打赏
  • 举报
回复
?!
chinajiji 2003-09-09
  • 打赏
  • 举报
回复
sevecol(sevecol.blogone.net) 的分析很正确!
我以前也遇到过类似的问题,只好改为pointer解决.
l1ul1u 2003-09-09
  • 打赏
  • 举报
回复
引用的错误
sevecol(sevecol.blogone.net) 说的很详细了
l1ul1u 2003-09-09
  • 打赏
  • 举报
回复
引用的错误
sevecol 2003-09-09
  • 打赏
  • 举报
回复
上面的是VC7.1里面的STL
sevecol 2003-09-09
  • 打赏
  • 举报
回复
会产生一个引用的引用的错误

mem_fun_ref(&Test::Heavier)会返回一个对象
return (std::const_mem_fun1_ref_t<_Result, _Ty, _Arg>(_Pm));我们需要关的是_Arg
这个_Arg (_Result (_Ty::*_Pm)(_Arg) const)就是你的Heavier的参数类型,这里是一个int&
我们看看std::const_mem_fun1_ref_t<_Result, _Ty, _Arg>的定义
template<class _Result,class _Ty,class _Arg>
class const_mem_fun1_ref_t: public binary_function<_Ty, _Arg, _Result>

派生自
template<class _Arg1,class _Arg2,class _Result>
struct binary_function

我们发现_Arg传递给了binary_function的_Arg2
在binary_function有这样的
typedef _Arg2 second_argument_type;
也就是说second_argument_type是int&了

回到
template<class _Fn2>
class binder2nd: public unary_function<typename _Fn2::first_argument_type,typename _Fn2::result_type>
{ // functor adapter _Func(left, stored)
public:
typedef unary_function<typename _Fn2::first_argument_type,
typename _Fn2::result_type> _Base;
typedef typename _Base::argument_type argument_type;
typedef typename _Base::result_type result_type;

binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

result_type operator()(const argument_type& _Left) const
{ // apply functor to operands
return (op(_Left, value));
}

result_type operator()(argument_type& _Left) const
{ // apply functor to operands
return (op(_Left, value));
}

protected:
_Fn2 op; // the functor to apply
typename _Fn2::second_argument_type value; // the right operand
};

里面的构造函数
binder2nd(const _Fn2& _Func,const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
的第二个参数类型是_Fn2::second_argument_type&
在这里_Fn2就是我们上面生成std::const_mem_fun1_ref_t<_Result, _Ty, _Arg>
这个的second_argument_type就是我们的_Arg就是int&

这里就出现了引用的引用.

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧