|
Boost Users : |
From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-12-21 15:14:44
Hi Andy,
----- Mensaje original -----
De: "Venikov, Andy" <avenikov_at_[hidden]>
Fecha: Viernes, Diciembre 21, 2007 5:48 pm
Asunto: [Boost-users] multi_index_container: modification of a non-key
member of a value_type
Para: boost-users_at_[hidden]
> Let say we have
>
> struct A {...}; struct B {...}; struct C {...};
>
> And we define a container of objects that hold these three
structures:
>
> struct Whole { A a; B b; C c; };
>
> multi_index_container<Whole,
> indexed_by< ordered_unique< member<Whole, A, &Whole::a> > >,
> indexed_by< ordered_unique< member<Whole, A, &Whole::b> > >
> >
> container;
This definition is incorrect, but I assume you mean the following:
multi_index_container<
Whole,
indexed_by<
ordered_unique< member<Whole, A, &Whole::a> >,
ordered_unique< member<Whole, B, &Whole::b> >
>
> container;
> As you can see only a and b are the keys, c is just a member.
>
> Let's also say that we've got an iterator "it" pointing to an
element
> in the container
>
> (through what index the iterator has been retrieved is immaterial at
> this point).
>
> And we want to modify "c" member of that element.
>
> Is this safe:
>
> *const_cast<C **>(&it->c) = <new value of c> ???
[...]
Well, the expression above is incorrect, I take you meant this
instead:
*const_cast<C *>(&it->c) = <new value of c>;
and yes, this is safe and works as intended, and as you point
out is more efficient (though more dangerous also) than using
modify() for the reasons you say.
An alternative is to define c as a mutable member of Whole,
in which case you don't need any const_cast to change the
value of this member.
Thanks for using Boost.MultiIndex,
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
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