Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Range fancify please
From: Eric Niebler (eric_at_[hidden])
Date: 2009-12-27 16:00:37


On 12/27/2009 7:01 PM, anony wrote:
> Eric Niebler pravi:
>> On 12/27/2009 4:33 PM, anony wrote:
>>> I have this variable definition:
>>>
>>> float vertices[][3] = {
>>> {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1},
>>> {-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}
>>> };
>>>
>>> I want to scale all elements of vertices, by some constant, for now I do
>>> it like this:
>>>
>>> BOOST_FOREACH(float& f, boost::make_iterator_range(
>>> boost::begin(vertices[0]), boost::end(vertices[7])))
>>
>> Yikes! You can't do that. The begin and end iterators point into
>> different arrays.
>>
>>> {
>>> f *= scale;
>>> }
>>>
>>> But maybe something more beautiful could be done with Boost.Range?
>
> Are you certain I am wrong? The array is multidimensional and contiguous
> in memory. The boost::begin() and boost::end() just return pointers. I
> have checked if all the floats scale correctly and they do.

I'm pretty certain. You're straying into undefined behavior by making a
range using iterators from different arrays.

>> Why not just use BOOST_FOREACH correctly?
>>
>> BOOST_FOREACH(float (&rgf)[3], vertices)
>> BOOST_FOREACH(float& f, rgf)
>> f *= scale;
>
> Double loop, oops. Not fancy enough.

Are you joking?

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.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