Boost logo

Boost Users :

From: Bill Somerville (bill_at_[hidden])
Date: 2008-05-05 20:30:05


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

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.

-- 
Bill Somerville
Class Design Limited


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