|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-09-17 07:47:27
From: "Shane Beasley" <sbeasley_at_[hidden]>
>
> I actually got this to work for a const member function:
>
> std::for_each(m.begin(), m.end(),
> boost::bind(&Foo::f,
> boost::bind(&std::map<int, Foo>::value_type::second, _1)));
>
> However, if Foo::f is a non-const member function, it still fails to
> compile. The error:
>
> /usr/include/boost/bind.hpp:163: initialization of non-const reference
> type `class Foo &'
> /usr/include/boost/bind.hpp:163: from rvalue of type `Foo'
> /usr/include/boost/bind/mem_fn_template.hpp:55: in passing argument 1 of
> `boost::_mfi::mf0<void,Foo>::operator ()(Foo &) const'
>
> Hmm.
Hmm indeed.
It doesn't work because boost::bind, for portability reasons, does not have
a proper return type inference mechanism, and cannot tell from the type of
the member pointer whether it will refer to a const object. Hence, the
return type of the nested bind is (supposed to be) considered conservatively
to be a const reference to a Foo.
The error message exposes a bug in bind, the return type was deduced as a
"Foo" instead of "Foo const &". With that fixed, the error message is even
worse, but at least the const version works as intended, not operating on a
copy.
Thanks for the bug report. :-)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk