Boost logo

Boost Users :

From: Max (LoadCom_at_[hidden])
Date: 2008-05-05 21:00:43


>#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 );
>}
>
Thank you for your reply, I've also walked through the brief documentation
of Ref (There's even not a simple example showing its usage and power!)

The solution you provided is nearly what I need. My main intention is to get
the ability to group/organize scattered/individual variables of a same type
into a standard container (vector or so) to enable fast/elegant traversal in
one run with one or two lines of code (with a call to for_each() or similar),
without doing the same operation one by one with these individual variables.

With your solution, I should declare my varaibles of type T, on which I will
do some operation in the future, as boost::reference_wrapper<T>. Obviously
to me for any arbitrary type, class reference_wrapper<T> will not automatically
delegate all the functionality of T. That's why I'm not saying this is EXACTLY
what I need. And remember to delcare T as reference_wrapper<T> for a variable
is an extra burden to me. :-)

In the meantile, your code is no different to the following much simpler form:

        boost::array<double, 2> array;

        double & d0 = array[0];
        double & d1 = array[1];

        array[0] = 1.0;
        array[1] = 2.0;
        assert( d1 == 2.0 );

I also tried the following code snnippet:

        typedef boost::reference_wrapper<double> double_ref_t;

        double d0, d1, d2, d3;
        boost::array<double_ref_t, 4> array = {{boost::ref(d0), boost::ref(d1), boost::ref(d2), boost::ref(d3)}};
         for(int i=0; i<4; i++) array[i] = 2.0;
         assert(d2 == 2.0);
        return 0;

It seems closer to what I need but, in fact, it has the same restriction. And, even worse, it didn't get compiled.

Thank you anyway for your help!
Still expecting more exiciting solutions!

B/Rgds
Max


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