Boost logo

Boost Users :

From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2005-04-05 14:37:10


Martin <adrianm <at> touchdown.se> writes:

>
> The only thing I am missing is a way to update the non-key elements in-place.
> Often my structures include subtables which means the copy can be expensive
> and I don't want to add member functions to update each member.
>
> Currently I do
>
> const_cast<std::vector<subtable>&>(itr->subtable).push_back(xxx);
>
> It would be nice if there was a macro or template that could do it in a safe
> way, i.e. ensure index isn't changed.
>
> nonkey(itr->subtable).push_back(xxx);
>

Well, you can build it yourself:

template<typename T>
T& deconst(const T& x)
{
  return const_cast<T&>(x);
}

deconst(itr->subtable).push_back(xxx);

But using this, you cannot really make the program
enforce the immutability of keys, so you're basically on
your being careful. No safe alternative that I know
of by way of const_casting.

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