https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85858

--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Mike Sharov from comment #8)
> My mental model here is actually of const correctness, not C++ specifically.
> When I pass around a const object I expect it to stay unmodified. Consider a
> function that takes a const T* argument. The signature suggests that the
> passed-in object will only be read and will not be modified.

No, no, no. It says **that function** can't modify it **through that pointer**.
If the pointee was not actually declared const then it can still be changed.

> If that
> function deletes the pointer, "bad things" will likely happen. Suddenly the
> object contains garbage and you can't figure out how it happened.

Your assumption that const T* means the pointer remains valid is just
completely unjustifiable:

   int* p = new int();
   const int* pc = p;
   delete p;
   // pc is valid here, nobody can delete it due to "const correctness" (wrong)

It's simply not how C++ works. An object's lifetime is distinct from it's
constness, and a pointer-to-const doesn't imply anything about whether the
pointed-to object is immutable.

Reply via email to