Using std::bind2nd with references

若如初见. 提交于 2020-01-01 06:27:17

问题


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

If I remove the reference in the parameter f() it compiles fine. 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.


回答1:


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). Boost would help - boost::bind supports references transparently, and there's also 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.




回答2:


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)

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));



回答3:


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". A conforming C++ implementation simply can't deal with reference parameters. mem_fun_ref returns an object of a class that has nested typedefs (

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. In case argument_type is a reference already this will fail to compile.

One solution might be to replace memfunref with your own template magic and strip away references for the nested argument_type typedefs.




回答4:


Actually, the compilers error message tells the whole story:

error C2529: '_Right' : reference to reference is illegal

std:: binders take their arguments as references - you cant pass a reference to a reference.

No way.



来源:https://stackoverflow.com/questions/1464439/using-stdbind2nd-with-references

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!