I've stumbled upon this code (simplified version here):
template<typename T>
class SmartPtr;
template<typename T>
struct SmartPtr<const T>
{
const T& operator*() const { return *_ptr; }
const T* operator->() const { return _ptr; }
const T* _ptr;
// more member data and functions here
// ...
};
template<typename T>
struct SmartPtr : public SmartPtr<const T>
{
T& operator*() const { return *const_cast<T*>(_ptr); }
T* operator->() const { return const_cast<T*>(_ptr); }
};
It's the first time I see a template class derived like this. Can anyone help me understand what one can achieve using this? To me it seems the general version behaves the same as the specialization if instantiated with a const T and the specialization is not necessary. Am I missing something?