inaccessible desctuctor warning a little overzealous

Mike Harrold mharrold@cas.org
Fri Jul 29 19:54:00 GMT 2005


I don't agree with this, although I would if the constructor was
also non-public. 

As it stands, it is possible to construct a copy of this class as
a local variable, but there is no way to destroy it. Therefore the
warning is correct.

/Mike

> 
> 
> --Apple-Mail-4-477986729
> Content-Transfer-Encoding: 7bit
> Content-Type: text/plain;
> 	charset=US-ASCII;
> 	format=flowed
> 
> The private destructor warning is a little overzealous in this case:
> 
> // { dg-options "-Wctor-dtor-privacy" }
> 
> class Foo {                             // { dg-bogus "" }
> public:
>    void release() { delete this; }
>    Foo() {}
> private:
>    ~Foo() {}
> };
> 
> int main() {
>    Foo* f = new Foo;
>    f->release();
> }
> 
> t.cc:3: warning: 'class Foo' only defines a private destructor and has 
> no friends
> 
> Ok?
> 
> 
> 
> --Apple-Mail-4-477986729
> Content-Disposition: attachment;
> 	filename=warn-1.diffs.txt
> Content-Transfer-Encoding: 7bit
> Content-Type: text/plain;
> 	x-unix-mode=0644;
> 	name="warn-1.diffs.txt"
> 
> 2005-07-29  Mike Stump  <mrs@apple.com>
> 
> 	* class.c (maybe_warn_about_overly_private_class): Don't warn if
> 	there is a way to call the dtor.
> 
> Doing diffs in .:
> --- ./cp/class.c.~1~	2005-07-26 15:28:36.000000000 -0700
> +++ ./cp/class.c	2005-07-29 12:38:24.000000000 -0700
> @@ -1528,17 +1528,22 @@ maybe_warn_about_overly_private_class (t
>  	}
>      }
>  
> -  /* Even if some of the member functions are non-private, the class
> -     won't be useful for much if all the constructors or destructors
> -     are private: such an object can never be created or destroyed.  */
> +  /* The class won't be useful for much if all the destructors are
> +     private: such an object can never be destroyed, well unless some
> +     of the member functions are non-private.  */
>    fn = CLASSTYPE_DESTRUCTORS (t);
> -  if (fn && TREE_PRIVATE (fn))
> +  if (! has_nonprivate_method
> +      && fn
> +      && TREE_PRIVATE (fn))
>      {
> -      warning (0, "%q#T only defines a private destructor and has no friends",
> +      warning (0, "%q#T only defines a private destructor and has no friends, static member functions or non-private methods",
>  	       t);
>        return;
>      }
>  
> +  /* Even if some of the member functions are non-private, the class
> +     won't be useful for much if all the constructors are private:
> +     such an object can never be created.  */
>    if (TYPE_HAS_CONSTRUCTOR (t))
>      {
>        int nonprivate_ctor = 0;
> --- ./testsuite/g++.old-deja/g++.mike/warn-9.C.~1~	2005-07-29 12:40:29.000000000 -0700
> +++ ./testsuite/g++.old-deja/g++.mike/warn-9.C	2005-07-29 12:37:42.000000000 -0700
> @@ -0,0 +1,14 @@
> +// { dg-options "-Wctor-dtor-privacy" }
> +
> +class Foo {				// { dg-bogus "" }
> +public:
> +  void release() { delete this; }
> +  Foo() {}
> +private:
> +  ~Foo() {}
> +};
> +
> +int main() {
> +  Foo* f = new Foo;
> +  f->release();
> +}
> --------------
> 
> --Apple-Mail-4-477986729--
> 
> 



More information about the Gcc-patches mailing list