This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/85858] -Weffc++ should not require copy ctor for const pointers
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 21 May 2018 19:40:14 +0000
- Subject: [Bug c++/85858] -Weffc++ should not require copy ctor for const pointers
- Auto-submitted: auto-generated
- References: <bug-85858-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85858
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Mike Sharov from comment #2)
> (In reply to Jonathan Wakely from comment #1)
> > (In reply to Mike Sharov from comment #0)
> > > When the pointer is const, it can not point to owned memory
> > Why not?
>
> Because a const pointer can not be freed.
That's what I thought you meant, and it's wrong.
> By "owned memory" I mean memory
> that was explicitly allocated by the object, which I assume was the
> situation that Effective C++ rule was referring to, or memory the ownership
> of which was passed to the object. In both cases the object has to keep a
> non-const pointer in order to be able to free it or to pass on the ability
> to free it to some other object.
Nothing stops you deallocating a const pointer.
struct X {
const int* const p;
X() : p(new int[10]) { }
~X() { delete[] p; }
};
This type should have a user-defined copy constructor (it doesn't need a
user-defined assignment operator though, because it's implicitly defined as
deleted).