sorted_vector, constness, pointers, and structs
Stephan Bergmann
sbergman at redhat.com
Thu Jul 19 00:45:27 PDT 2012
On 07/18/2012 08:09 PM, Lubos Lunak wrote:
> What you want is broken. It is definitely broken from the theoretical point
> of view, since modifying the items may change the sort order.
While one can argue that it is broken, in practice I would not bother
trying too hard to make the code safe against shooting-in-one's-foot at
the expense of simplicity. Whether or not making non-const element
access available for sorted_vector<T,C> is safe depends on both T
(element type) and C (comparison function), and e.g. adding a
specialization so that sorted_vector<T*> only gives access to T const*
can be OK or overly restrictive.
Quoting an answer I inadvertently only sent to Noel directly:
"Yeah, the 'only give const element access to avoid element
modifications that invalidate container invariants' strategy is
apparently not perfect.
"If you do need non-const access, there's no way around it than to offer
it---and being careful not to accidentally invalidate the container's
sortedness."
Stephan
More information about the LibreOffice
mailing list