D
Derek
Something puzzles me about Boost's shared_ptr implementation:
template <typename T>
class shared_ptr
{
public:
T* operator->() const
{
return p;
}
private:
T* p;
};
Why is operator->() const but the return value is non-const? The
Boost documentation answers this very question as follows:
"Shallow copy pointers, including raw pointers, typically don't
propagate constness. It makes little sense for them to do so, as you
can always obtain a non-const pointer from a const one and then
proceed to modify the object through it."
I don't understand this answer. Are they referring to using
const_cast to subvert the constness of shallow copy pointers? If so,
that seems like a weak argument for not enforcing const corectness.
Any insights appreciated.
template <typename T>
class shared_ptr
{
public:
T* operator->() const
{
return p;
}
private:
T* p;
};
Why is operator->() const but the return value is non-const? The
Boost documentation answers this very question as follows:
"Shallow copy pointers, including raw pointers, typically don't
propagate constness. It makes little sense for them to do so, as you
can always obtain a non-const pointer from a const one and then
proceed to modify the object through it."
I don't understand this answer. Are they referring to using
const_cast to subvert the constness of shallow copy pointers? If so,
that seems like a weak argument for not enforcing const corectness.
Any insights appreciated.