Consider the following type:
template<typename T> struct View
{
T* data;
size_t size;
};
Is it valid to cast from: View<T>&
To View<const T>&
?
Does it invoke undefined behaviour?
I suspect the cast will work as intended on all compilers but will be technically undefined behaviour according to the standard due to the strict aliasing rule.
View<T>
toView<const T>
.foo<T>&
tofoo<const T>&
is not exempt from strict aliasing rules, they are unrelated types. You could castdata
individually toconst T*
but that isn't the same as casting a reference to the whole struct.shared_ptr
has special casts that cast its inner pointer instead of the outer pointer.