Naveen
Naveen

Reputation: 73443

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

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.

Upvotes: 3

Views: 2114

Answers (4)

sellibitze
sellibitze

Reputation: 28097

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.

Upvotes: 1

Pavel Minaev
Pavel Minaev

Reputation: 101565

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.

Upvotes: 5

RED SOFT ADAIR
RED SOFT ADAIR

Reputation: 12218

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.

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264381

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

Upvotes: 2

Related Questions