
Hi Andy, ----- Mensaje original ----- De: "Venikov, Andy" <avenikov@reefpoint.com> 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@lists.boost.org
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