Boost logo

Boost Users :

From: Max (LoadCom_at_[hidden])
Date: 2008-05-05 21:11:49


>Hi Max & Ravi
>
>Ravi wrote:
>> On Saturday 03 May 2008 09:12:01 pm Max wrote:
>>
>>>>> double d0, d1, d2, d3;
>>>>> boost::array<double &, 4> array = {{d0, d1, d2, d3}};
>>>>> for(int i=0; i<4; i++) array[i] = i;
>>>>> assert(d2 == 2);
>>>>>
>>
>> [snip]
>>
>>
>>> If it won't take a long time, is it possible to rewrite the above code
>>> snnipet making use of boost.Ref?
>>>
>>
>> #include <boost/array.hpp>
>> #include <boost/ref.hpp>
>>
>> #include <cassert>
>>
>> int main()
>> {
>> typedef boost::reference_wrapper<double> double_ref_t;
>> boost::array<double, 2> array;
>>
>> double_ref_t d0( array[0] );
>> double_ref_t d1( array[1] );
>>
>> array[0] = 1.0;
>> array[1] = 2.0;
>> assert( d1 == 2.0 );
>>
>> d0 += d1;
>> assert( d0 == 3.0 );
>> }
>>
>or even:
>
>#include <cassert>
>#include <boost/array.hpp>
>#include <boost/ref.hpp>
>
>int main()
>{
> double d0, d1, d2, d3;
> boost::array<boost::reference_wrapper<double>, 4> array =
>{boost::ref(d0), boost::ref(d1), boost::ref(d2), boost::ref(d3)};
> for(int i=0; i<4; i++)
> {
> array[i].get() = i;
> }
> assert(d2 == 2);
>
> return 0;
>}

Thank you Somerville for your solution. This is exactly what I need!

>
>although this is a slightly unusual construct which might suffer nasty
>aliasing issues, perhaps a few more details of your requirement might
>clarify a better approach.

Please decorate on the 'nasty aliasing issues'. For more details of my
intention, please refer to my previous mail I just posted. I hope ther's
a even more elgent way to get rid of the issues you mentioned.

B/Rgds
Max

>
>--
>Bill Somerville
>Class Design Limited
>
>
>_______________________________________________
>Boost-users mailing list
>Boost-users_at_[hidden]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users


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