This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/85858] -Weffc++ should not require copy ctor for const pointers


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).

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]