The casting function you are considering & hellip;
template< class T > inline QList<T const*>& constList( QList<T*> const& list ) { return reinterpret_cast< QList<T const*>& >( const_cast< QList<T*>& >( list ) ); }
& hellip; can be practical (perhaps QList does not change its representation of the object depending on the const -ness of the element type), but it can violate the correctness of const .
Firstly, since dropping the const -ness of the list itself does not match const : it allows you to change the original const list.
But even if this formal argument const removed, for example, & hellip;
template< class T > inline QList<T const*>& constList( QList<T*>& list ) { return reinterpret_cast< QList<T const*>& >( list ); }
& hellip; there is still a const correctness problem.
The reason is that the list is an additional level of indirection, and your function itself is not const . Thus, after using your function to get a link to a list with supposed const pointer elements, you can store a pointer to what const in this list. And then you can use the original list to change this const , bang element.
For the same reason that there is no implicit conversion from T** to T const** .
What you can do without such problems is that with the already specified const list of pointers to objects, they are forced to point to const objects:
template< class T > inline QList<T const*> const& constList( QList<T*> const& list ) { return reinterpret_cast< QList<T const*> const& >( list ); }
Formally, reinterpret_cast still exists as a potential problem, but any specialist who specializes in presenting QList on constness of elements seems to deserve what he received. :-)
Cheers and hth.,