std::set<int * const> won't compile
You cannot modify elements stored in an std::set
so the point is moot. It is designed to keep elements in a sorted order and modifications would break that guarantee. That's why the iterators (both std::set<T>::iterator
and std::set<T>::const_iterator
) both return const references.
There is no way to edit an element short of mutable
(or const_cast
), in which case you still need to guarantee the ordering remains the same.
Here's a simple program to demonstrate the problem you are seeing:
int main(int argc, char ** argv)
{
int * const a = NULL;
int * const b = NULL;
b = a; // error: cannot assign to variable 'b' with const-qualified type
}
Note that it's a compile-time error to change the value of a variable of int * const
, because the variable is considered read-only.
std::set
internally has the same problem -- it needs to modify variables of the specified type, and it cannot do so if its specified type is read-only.
Changing the type to const int *
instead is probably what you want to do, as that type allows the pointers to be overwritten when necessary (while not allowing modifications to the int
s that they point to).