I have a simple vector implementation in C, which holds an array of void*. It's up to the user to take care of the actual type.
I want to 'promise' that the vector will not alter its contents, so I store data as:
Struct _Vector{
UInt32 used;
UInt32 size;
const void** arr;
};
I don't know if it's considered "overdoing it" with my constness correctness, but I try to maintain const correctness with my accessors/modifiers:
void vector_add(Vector *v, const void* const elem);
void vector_set(Vector *v, const UInt32 idx, const void* const elem);
When I return an element from the vector, it is a pointer to the user's data so I let the user modify the data being pointed to, by casting away the constness in the internal array.
void* vector_get(const Vector *v, const UInt32 idx){
if ( idx >= v->used )
exitAtError("Vector","Array out of bounds");
return (void*)v->arr[idx];
}
Instead of returning a const void*, I return a void* instead. The idea is that internally, I want to ensure that I am not changing the data being pointed to, but I don't care what happens to the data outside the vector.
Is this considered a correct use of const? I would think it's okay to declare data as const in one place, while having it be mutable elsewhere. I'm unsure after reading many dogmatic articles claiming never to cast away constness, and if it is casted away, then the use of const was not appropriate to begin with.