sorted_vector, constness, pointers, and structs

Noel Grandin noelgrandin at gmail.com
Wed Jul 18 10:52:24 PDT 2012


On Wed, Jul 18, 2012 at 7:23 PM, Lubos Lunak <l.lunak at suse.cz> wrote:
>
>  But it is correct that you get an error in the second case and you ideally
> should be getting one in the first case as well. You cannot modify the
> elements of the vector because that might modify what defines their position
> in their sorted order, isn't that so? That is exactly the reason for those

Ummm. No.
The sorted_vector class is not intended for storing primitive values.
Virtually all of the use-cases create a custom comparator operation.

Almost all of time, we are storing complex structs/classes in these
vectors, and they have numerous fields that can and do need to be
easily modifiable.
Returning const-only references defeats that.

So we end up having to cast away the const-ness most of the time,
which makes the supposed protection worthless.


More information about the LibreOffice mailing list