I am given a pointer: T* (where T is a non-CV type). I do not know whether it points to a valid object or not. It can be given any value and it may be valid or not. I would like to add constness to it using const_cast:
Is this allowed? Notice that I do not access this object. The object is accessed later (proper validation is done at run-time).Code:T* t1 = <possible invalid address, not necessarily null>; const T* t2 = const_cast<const T*>(t1);
I have been looking in the standard and I could not find the answer (const_cast for NULL is defined, but for other addresses?).
I know that in practice it is allowed (since it does not access any v-table or such). Boost's weak_ptr also makes such an assumption even for other casts, so I am going to implement it too, but anyway, I would like to know the answer.