For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2006 > Can't use function binder with reference parameter









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can't use function binder with reference parameter
Alex Blekhman

2006-03-16, 7:01 pm

Hello,

I'm trying to use STL binders with my class. However,
binders can bind only values to parameters. Reference cannot
be bound:

using namespace std;

class X {} x;
typedef binary_function<int, X&, bool> Fn2;

Fn2 f; // bool f(int, X&)
binder2nd<Fn2> b = bind2nd(f, x); // <-- C2529 error

I get C2529 error:

error C2529: reference to reference is illegal

in <functional> header when compiler attempts to construct
binder2nd.

How can I overcome this problem besides converting second
parameter to pointer?

Thanks in advance
Alex


Tom Widmer [VC++ MVP]

2006-03-17, 7:58 am

Alex Blekhman wrote:
> Hello,
>
> I'm trying to use STL binders with my class. However,
> binders can bind only values to parameters. Reference cannot
> be bound:
>
> using namespace std;
>
> class X {} x;
> typedef binary_function<int, X&, bool> Fn2;
>
> Fn2 f; // bool f(int, X&)
> binder2nd<Fn2> b = bind2nd(f, x); // <-- C2529 error
>
> I get C2529 error:
>
> error C2529: reference to reference is illegal
>
> in <functional> header when compiler attempts to construct
> binder2nd.
>
> How can I overcome this problem besides converting second
> parameter to pointer?


There is a special implementation of <functional> that gets around this
reference-to-reference problem:
http://www.boost.org/libs/functional/index.html

However, I suspect that in the next C++ standard the binders will be
deprecated in favour of boost::bind (from the same site), soon to be
std::tr1::bind. Adding boost to your code base usually greatly improves
code quality and reduces development time and bugs.

Tom
Alex Blekhman

2006-03-17, 7:01 pm

Tom Widmer [VC++ MVP] wrote:
>
> There is a special implementation of <functional> that
> gets around this reference-to-reference problem:
> http://www.boost.org/libs/functional/index.html
>
> However, I suspect that in the next C++ standard the
> binders will be deprecated in favour of boost::bind (from
> the same site), soon to be std::tr1::bind. Adding boost
> to your code base usually greatly improves code quality
> and reduces development time and bugs.


Thanks for the answer. Yes I'm aware of boost solution.
Unfortunately I cannot change existing project, so I'm
limited to current STL implementation. Meanwhile I'm using
pointer instead of reference as function parameter. Also,
lack of select1st/select2nd together with compose is pain
when one's using for_each with maps.

Thanks
Alex


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com