# std::bind2nd reference to reference issues

This topic is 4757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I have:
class Foo
{
public:
void MemberFunction(const int& value) //int is used here, but this problem happens with any type
{
}
};

int main()
{
std::list<Foo> aList;

//assume aList gets filled up with elements or something

std::for_each(aList.begin(), aList.end(), std::bind2nd(std::mem_fun(&Foo::MemberFunction), 5));

return 0;
}


VS.Net 2003 gives me a "reference to reference" compiler error. If I make Foo::MemberFunction accept a value by copy instead of reference it compiles fine. Is there a way to make this work when I have the member function accept a reference using the STL? If not, what's the rationale behind it, and can boost::bind do this?

##### Share on other sites
If you look at the decleration of the binder2nd class, you will see that no, it is not possible the constructor in question being:
binder2nd(      const Operation& _Func,      const typename Operation::second_argument_type& _Right      );

As you can see, the second argument is a reference type of the second argument of the function you pass, which will be a reference, and references of references are illegal.

As far as boost::bind goes, try it and see.

##### Share on other sites
Quote:
 Original post by WashuIf you look at the decleration of the binder2nd class, you will see that no, it is not possible the constructor in question being:binder2nd( const Operation& _Func, const typename Operation::second_argument_type& _Right );As you can see, the second argument is a reference type of the second argument of the function you pass, which will be a reference, and references of references are illegal.As far as boost::bind goes, try it and see.

Darn. Surely this makes std::bind2nd really rather limited? Oh well, boost::bind does this fine.

##### Share on other sites
Quote:
 Original post by load_bitmap_fileDarn. Surely this makes std::bind2nd really rather limited? Oh well, boost::bind does this fine.

It's known technical issue in the current standard itself that will be resolved, boost provides exact equivalents of the standard library function object adaptors and member function adaptors (like boost::bind2nd) that resolve these issues check Boost.Functional (not boost::function). However none of them are as powerful, flexible as boost::bind & placeholders, boost::bind even gives a uniformity which is always a plus.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632930
• Total Posts
3009290
• ### Who's Online (See full list)

There are no registered users currently online

×