sorted_vector, constness, pointers, and structs

Noel Grandin noel at
Wed Jul 18 07:56:12 PDT 2012


So David and Stephan recommended that I make the accessor methods to 
o3tl::sorted_vector const in order to prevent clients from invalidating 
the sorted-ness of it.
This works out fine when I'm storing pointers to something in the 
sorted_vector like this:

struct SomeStruct {
     int xxx;
o3tl::sorted_vector<SomeStruct*>  var1;
var1[0]->xxx = 12;

Which is because thanks to C++'s typing rules, const-ness does not 
propogate with a pointer.
However, if I do this:

o3tl::sorted_vector<SomeStruct>  var1;
var1[0].xxx = 12; // <--- error! reference is const!!!

And, in fact, if I store any value class directly into 
o3tl::sorted_vector, it becomes pretty much useless, because I can't get 
anything out of it afterwards without casting away the const-ness.

What is the "correct C++ idiom" here?

Should I create a separate container types here?
And if so, what would be a good names to distinguish between the "sorted 
vector for pointers to values" and the "sorted vector for values" ?
I don't want to use sorted_ptr_vector, because that sounds too much like 
a boost::ptr_vector, and might confuse people as to the ownership 
semantics of this thing.

Thanks, Noel Grandin


More information about the LibreOffice mailing list