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
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