mem_fun_ref 麻烦
我无法弄清楚 mem_fun_ref
。 我必须承认,我通常使用函子来做这种事情,因为它们可以内联以提高速度和利润。 然而,这段代码不会成为瓶颈,所以我想尝试一下这个东西。
这是我想做的一个例子。 我知道还有其他方法可以做到这一点。 我不想使用copy
,我不想使用范围成员函数,我不想使用back_inserter
。 我特别想使用mem_fun_ref
。 这只是一个简单的例子,实际情况要复杂得多。 也就是说,我真的不知道为什么这是错误的,但我不熟悉 mem_fun_ref
或 mem_fun
。
这就是我想要的工作:
#include <list>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
list<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
vector<int> b;
// should work like magic!
for_each(a.begin(), a.end(), bind1st(mem_fun_ref(&vector<int>::push_back), b));
}
但我收到 3 个错误:
1>c:\program files\microsoft visual studio 9.0\vc\include\functional(276) : error C2529: '_Right' : reference to reference is illegal
1>c:\program files\microsoft visual studio 9.0\vc\include\functional(281) : error C2529: '_Right' : reference to reference is illegal
1>c:\program files\microsoft visual studio 9.0\vc\include\functional(282) : error C2535: 'void std::binder1st<_Fn2>::operator ()(const int &(&)) const' : member function already defined or declared
1> with
1> [
1> _Fn2=std::mem_fun1_ref_t<void,std::vector<int>,const int &>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\functional(276) : see declaration of 'std::binder1st<_Fn2>::operator ()'
1> with
1> [
1> _Fn2=std::mem_fun1_ref_t<void,std::vector<int>,const int &>
1> ]
对引用的引用是非法的
让我认为该函数需要按值获取参数。 但是,当然,这是不可能在 vector
中更改的,并且也不可能在我的代码中更改它。 有没有一个简单的改变可以让它发挥作用? 我需要一个单行解决方案。
I am having trouble figuring out mem_fun_ref
. I have to admit, I usually use functors for this kind of thing, since they can be inlined for speed and profit. However, this code is not going to be a bottleneck and so I wanted to try this thing.
Here is an example of what I want to do. I know there are other ways to do it. I don't want to use copy
, I don't want to use range member functions, I don't want to use a back_inserter
. I specifically want to use mem_fun_ref
. This is just a simple example, the real case is much more complicated. That said, I really don't know why this is wrong, but I am not familiar with mem_fun_ref
or mem_fun
.
Here's what I want to work:
#include <list>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
list<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
vector<int> b;
// should work like magic!
for_each(a.begin(), a.end(), bind1st(mem_fun_ref(&vector<int>::push_back), b));
}
But I get 3 errors:
1>c:\program files\microsoft visual studio 9.0\vc\include\functional(276) : error C2529: '_Right' : reference to reference is illegal
1>c:\program files\microsoft visual studio 9.0\vc\include\functional(281) : error C2529: '_Right' : reference to reference is illegal
1>c:\program files\microsoft visual studio 9.0\vc\include\functional(282) : error C2535: 'void std::binder1st<_Fn2>::operator ()(const int &(&)) const' : member function already defined or declared
1> with
1> [
1> _Fn2=std::mem_fun1_ref_t<void,std::vector<int>,const int &>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\functional(276) : see declaration of 'std::binder1st<_Fn2>::operator ()'
1> with
1> [
1> _Fn2=std::mem_fun1_ref_t<void,std::vector<int>,const int &>
1> ]
reference to reference is illegal
makes me think that the function needs to take a parameter by value. But of course, this is not possible to change in vector
, and it's not possible to change it in my code either. Is there a simple change to get this to work? I need a solution that's a 1-liner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用
绑定
即可。mem_fun
版本太难了。另一种不需要使用 ref 的方法是将指针传递给要修改的向量:
Just use
bind
. Themem_fun
versions are too difficult.Another way that doesn't require the use of
ref
is to pass a pointer to the vector to be modified:Herb Sutter 在“Exceptional C++ Style”第 28-30 页中解释了这个问题。 人们可能无法安全地创建指向
vector::push_back
方法的指针,因为需要确保成员函数的准确签名,即使对于vector
来说,这也可能并不明显。标准库中的 int>::push_back
。 这是因为(在标准库中):最后,Herb Sutter 建议
This problem was explained in "Exceptional C++ Style" by Herb Sutter, page 28-30. One probably cannot safely create a pointer to a
vector<int>::push_back
method as one needs to be sure of exact signature of the member function, which may not be obvious even forvector<int>::push_back
in Standard Library. This is because (in Standard Library):In the end, Herb Sutter advised that
我知道您说过您不想使用
back_inserter
,可能是因为您只提供了简化的示例代码。对于其他想知道如何准确执行您想要执行的操作并乐意使用它的人,请使用
back_inserter
:I know that you've said you don't want to use
back_inserter
, probably because you've given just simplified example code.For anyone else wondering how to do exactly what you're trying to do, and happy to use it, use
back_inserter
:也就是说,总有
other_mem_fun
,这是我在知道 boost 之前就编造的。 这可能适合。That said, there's always
other_mem_fun
, which I cooked up before I knew about boost. This might fit.