Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-01-04 14:51:33


From: "Duane Murphy" <duanemurphy_at_[hidden]>
> Bind has been working very well for some of the things I have been
> wanting to do. I have run into a rather puzzling problem this time and
> was wondering what was happening:
>
> Here is the example:
>
> void f( int )
> {
> }
>
> struct array_obj
> {
> int operator[]( int ){ return NULL; }
> };
>
> int main()
> {
> array_obj a;
> int array[1];
> int value = a[0];
> boost::bind( f, _1 )( 3 );
> boost::bind( f, _1 )( value );
> boost::bind( f, _1 )( array[0] );
> boost::bind( f, _1 )( a[0] );
> return 0;
> }
>
> The first three bind statements compile just fine. The third one issues
> an error:
>
> function call 'operator()(int)' does not match.
>
> Along with all 20 variations of operator() generated by bind.
>
> Obviously there is something amiss with going through the return value of
> operator[](). I also get the same problem using an iterator which would
> be going through operator*().

The problem here is that a[0] is an rvalue of type int. This is a known
limitation of bind(); it needs to pass the arguments unmodified to f (since
f may have a reference argument) and uses signatures of the form

template<class T> result_type operator()(T & t) const;

Unfortunately this doesn't work for rvalues (temporaries.) I don't know of a
good solution.

Making operator[] return int & (or int const & in the const case) would
solve this specific problem, if that's an option.

--
Peter Dimov
Multi Media Ltd.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net