Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-01-11 06:50:02


Wu Yinghui, Freddie wrote:

> struct Operator
> {
> Operator &compute(Argument const &arg)
> {
> std::cout << arg.value_ << std::endl;
> return *this;
> }
> // Warning is gone if I declare the above function as:
> //Operator const &compute(Argument const &arg) const {...}
> };
>
> int main()
> {
> typedef std::map<int, Operator> OperatorMap;
>
> OperatorMap ops;
> ops.insert(std::make_pair(0, Operator()));
>
> Argument arg0 = { 1 };
>
> std::for_each(
> ops.begin(), ops.end(),
> boost::bind(&Operator::compute,
> boost::bind(&OperatorMap::value_type::second,
> _1), boost::cref(arg0)));
> }

The problem is that

    boost::bind(&OperatorMap::value_type::second, _1)

returns an Operator by value (in 1.33.1) or by const reference (in the
current CVS). Since boost::bind needs to decide on a result type at bind
time, and since it doesn't know whether the argument coming in from the _1
is const, it needs to conservatively assume const (Lambda doesn't have this
limitation because it deduces the return type at call time).

Use

    boost::bind<Operator&>(&OperatorMap::value_type::second, _1)

to tell boost::bind to return a non-const reference.


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