Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-05-23 20:35:39


Daniel Gehriger wrote:
> I tried BOOST_FOREACH on a slightly modified version of the sample in the
> documentation. Instead of iterating by value, I iterate by reference:
>
> extern std::vector<float> get_vector_float();
> BOOST_FOREACH( float& f, get_vector_float() )
> {
> }
>
> When compiling with VisualC++ 8.0, I receive the following error message:
>
> error C2440: 'initializing' : cannot convert from 'const float' to 'float &'
> Conversion loses qualifiers
>
> Is there a work-around ?

This is by design. get_vector_float() returns a temporary object. You
can't modify it. You can get const references to the floats:

BOOST_FOREACH( float const & f, get_vector_float() )
{
}

or you can copy the vector into a local and iterate over that:

std::vector<float> v = get_vector_float();
BOOST_FOREACH( float & f, v )
{
}

HTH,

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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