[英]Using std::bind2nd with references
I have a simple class like this: 我有一个简单的课程,像这样:
class A
{
public:
void f(const int& n)
{
std::cout<<"A::f()" << n <<"\n";
}
};
and I am trying to use it like this: 我正在尝试像这样使用它:
std::vector<A> vec;
A a;
vec.push_back(a);
std::for_each(vec.begin(), vec.end(), std::bind2nd(std::mem_fun_ref(&A::f), 9));
But when I compile the code I get the following error somewhere inside functional header file: 但是,当我编译代码时,在函数头文件中的某处出现以下错误:
error C2529: '_Right' : reference to reference is illegal
错误C2529:“ _ Right”:对引用的引用是非法的
If I remove the reference in the parameter f() it compiles fine. 如果我删除参数f()中的引用,它将编译良好。 How do I resolve this?
我该如何解决? I don't want to remove the reference as in my real code the copying of the object is quite costly.
我不想删除引用,因为在我的真实代码中,对象的复制非常昂贵。 Also, I am not using boost.
另外,我没有使用boost。
You can't do that easily, sorry. 对不起,您无法轻松做到。 Just consider it one of those cases not covered by
std::bind1st
and std::bind2nd
(kinda like 3-argument functions etc). 只需考虑一下
std::bind1st
和std::bind2nd
未涵盖的情况之一(有点像3参数函数等)。 Boost would help - boost::bind
supports references transparently, and there's also boost::ref
. Boost会有所帮助
boost::bind
透明地支持引用,并且还有boost::ref
。
If your implementation supports TR1 - latest g++ versions and VC++2008 SP1 both do - then you can use std::tr1::bind
, which is for the most part same as boost::bind
, but standardized. 如果您的实现支持TR1-最新的g ++版本和VC ++ 2008 SP1都支持-那么您可以使用
std::tr1::bind
,其大部分与boost::bind
相同,但是是标准化的。
I dont believe you can bind parameters to a method that takes references. 我不相信您可以将参数绑定到需要引用的方法。 (not in the STL, I think the boost versions may let you do it but I am not sure)
(不在STL中,我认为增强版本可能会让您这样做,但我不确定)
You will need to roll your own. 您将需要自己滚动。
struct CallF
{
CallF(int const& data): m_data(data) {}
void operator()(A& val) const
{
val.f(m_data);
}
int const& m_data;
};
Use like this: 像这样使用:
std::for_each(vec.begin(), vec.end(), CallF(9));
I've been bitten by the same problem. 我被同样的问题咬了。 If you look into the C++ standard, you'll see that it's actually a "library defect".
如果查看C ++标准,您会发现它实际上是一个“库缺陷”。 A conforming C++ implementation simply can't deal with reference parameters.
合格的C ++实现根本无法处理参考参数。 mem_fun_ref returns an object of a class that has nested typedefs (
mem_fun_ref返回具有嵌套typedef的类的对象(
argument_type, first_argument_type, second_argument_type
) where references are not stripped away. )的引用没有被删除。 bind1st and bind2nd are specified to have an operator() wich takes references as parameters.
指定bind1st和bind2nd具有一个operator(),该操作符将引用作为参数。 In case argument_type is a reference already this will fail to compile.
如果arguments_type已经是引用,则将无法编译。
One solution might be to replace memfunref with your own template magic and strip away references for the nested argument_type typedefs. 一种解决方案可能是用您自己的模板魔术替换memfunref,并删除嵌套的arguments_type typedefs的引用。
Actually, the compilers error message tells the whole story: 实际上,编译器错误消息说明了整个故事:
error C2529: '_Right' : reference to reference is illegal
错误C2529:“ _ Right”:对引用的引用是非法的
std:: binders take their arguments as references - you cant pass a reference to a reference. std ::活页夹将其参数用作引用-您不能将引用传递给引用。
No way. 没门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.