返回   cpper编程论坛 > 技术杂烩
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

技术杂烩 找不到地方的技术问题?这里!

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2004-04-25
高级会员
 
注册日期: 2002-11-22
帖子: 249
clip 正向着好的方向发展
默认 question about reference parameter through bind2nd

I got some trouble to pass a parameter as reference through bind2nd, I am even not sure it's because bind2nd or mem_fun_ref, or conspiracy of both. I embeded my question into comment.
Thanks for help!

代码:
#include <functional> #include <algorithm> #include <vector> #include <iostream> using namespace std; struct A { void funcv (int a) { cout << a << endl; } void funcr (int& a) { cout << a << endl; } }; int main() { vector<A> c; c.push_back(A()); int g = 123; // this is ok for_each(c.begin(),c.end(),bind2nd(mem_fun_ref(&A::funcv),g)); // this is not ok, // neither for vc2003 nor for g++ 3.2 // so, my question is : is it possible to pass the parameter as reference for bind2nd // if yes, how to? // for_each(c.begin(),c.end(),bind2nd(mem_fun_ref(&A::funcr),g)); // Thanks. }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2004-04-25
高级会员
 
注册日期: 2002-11-22
帖子: 249
clip 正向着好的方向发展
默认 didn't figure out the answer, but one step further

it has nothing to do with using mem_fun_ref, the following code without mem_fun_ref fails too
代码:
#include <functional> #include <algorithm> #include <vector> using namespace std; struct A {}; struct my_functor_v:public binary_function<A,int,void> { void operator()(A a,int t)const{} }; struct my_functor_r:public binary_function<A,int,void> { void operator()(A a,int& t)const{} }; int main() { vector<A> c; c.push_back(A()); int g = 123; for_each(c.begin(),c.end(),bind2nd(my_functor_v(),g)); // seems it's just bind2nd, have nothing to do with mem_fun_ref // for_each(c.begin(),c.end(),bind2nd(my_functor_r(),g)); return 0; }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2004-04-25
高级会员
 
注册日期: 2003-11-11
帖子: 147
ilovecpp 正向着好的方向发展
默认

binder2nd holds the second argument by value. Use pointer or boost::ref in place of non-const reference in this situation.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2004-04-26
高级会员
 
注册日期: 2002-11-22
帖子: 249
clip 正向着好的方向发展
默认

sure, the simplest solution to my oringal problem is just give up for_each, and write an explict for loop, call member function directly. I just want to make sure I didn't miss some simple solution which already provided in stl to allow me still use for_each in this case.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2004-04-26
高级会员
 
注册日期: 2003-11-11
帖子: 147
ilovecpp 正向着好的方向发展
默认

Should have made it clearer: you don't have to give up for_each. Just change my_functor_r to:
代码:
struct my_functor_r:public binary_function<A,int*,void> or struct my_functor_r:public binary_function<A,boost::reference_wrapper<int>,void>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #6 (permalink)  
旧 2004-04-26
高级会员
 
注册日期: 2002-11-22
帖子: 249
clip 正向着好的方向发展
默认

i see. thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用


相似的主题
主题 主题作者 版面 回复 最后发表
question about auto_ptr clip 技术杂烩 2 2004-03-24 12:30 PM


所有时间均为格林尼治时间 +9。现在的时间是 08:03 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0