singleton idiom evokes a warning, an error.

Michael Cook cook@clearviewtech.com
Mon Dec 7 19:58:00 GMT 1998


Here are a couple really old bugs (they predate egcs).  They're both
related to the ubiquitous singleton idiom (where you make the ctor
and dtor private, and then have a static member function that
returns the "only" instance of the class).

class x
{
  x();
  ~x(); //4
  public: static x& only();
}; //6

x& x::only()
{
  static x t; //10
  return t;
}

/*
$ egcs -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.90.29/specs
gcc version egcs-2.90.29 980515 (egcs-1.0.3 release)
$ egcs -c -Wall blue.cpp -o /dev/null
blue.cpp:6: warning: `class x' only defines a private destructor and has no friends
blue.cpp: In function `void __tcf_0()':
blue.cpp:4: `x::~x()' is private
blue.cpp:10: within this context
$
*/

First "bug": the warning about "no friends".  The compiler is
failing to notice that we have a static member function.  If having
a friend would be enough to suppress this warning, then having a
static member function should be enough, too, no?

Second bug: the error about the dtor being private.  The compiler is
rejecting valid code.  Before you tell me I'm wrong about this one,
please read the following message (from 14 months ago):

|From: kanze@gabi-soft.fr (J. Kanze)
|Message-ID: <m3k9fc25dj.fsf@gabi-soft.fr>
|Date: 17 Oct 97 16:45:01 GMT
|Newsgroups: comp.std.c++
|Subject: Re: private dtor, static instance
|Organization: GABI Software, S`rl.
|Approved: Fergus Henderson <fjh@cs.mu.oz.au>
|
|Michael R Cook <mcook@cognex.com> writes:
|
| |> If my class's destructor is private, does that mean I'm not allowed
| |> to create static instances of that of that class?
| |> 
| |> For example:
| |> 
| |>   class C {
| |>     static C& only();
| |>     C();
| |>     ~C();
| |>   };
| |>   C& C::only()
| |>   {
| |>     static C c; // error?
| |>     return c;
| |>   }
| |> 
| |> The dwp (Nov96) says:
| |> 
| |> 12.4 Destructors                                         [class.dtor]
| |> 10 Destructors are invoked implicitly (1) for a constructed object
| |>    with static storage duration (_basic.stc.static_) at program
| |>    termination (_basic.start.term_), (2) [...].  A program is
| |>    ill-formed if the destructor for an object is implicitly used and
| |>    it is not accessible (_class.access_).
| |> 
| |> But it doesn't say what context is used to determine the
| |> accessibility of the destructor that would be invoked at program
| |> termination.  I would expect that the construction context would be
| |> used.  (Microsoft agrees.  Gcc disagrees.)
|
|This has been clarified since the CD2.  The context for the
|accessibility check of the destructor is the same as the declaration, so
|the above code is legal.
|
|Since this clarification is *very* recent, don't expect all compilers to
|implement it yet.
|
|-- 
|James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
|GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
|        I'm looking for a job -- Je recherche du travail

Michael.



More information about the Gcc-bugs mailing list