Topic: singleton in standard


Author: kgw-zamboni-news@stiscan-zamboni.com
Date: Sun, 21 Apr 2002 19:24:47 GMT
Raw View
There is also the problem that there are three kinds of singletons .
One per thread, one per application and one per system (shared library).
Each one is solved differently.
=FF
=FF
Remove -zamboni to reply
All the above is hearsay and the opinion of no one in particular

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "mackrune" <rune420@start.no>
Date: Thu, 18 Apr 2002 07:25:33 GMT
Raw View
"chris" <chris@bubblescope.net> skrev i melding
news:a9ich6$g9l$1@pegasus.csx.cam.ac.uk...
> Hello!
>
> After my earlier thoughts about a "static class", I've realised that what
I
> really want in "comp. sci. terms" is a way of defining a class as a
> singleton. Doing so allows extra optimisations to be performed on the
> class...
>
> I can see two ways of doing this:
>
> 1) define "singleton" as a protected word and allow "singleton class"
>
> 2) allow a section of a namespace to be "private", and using a namespace
as
> a singleton in a quite nice looking manner.


Sounds like a good idea, but you could basically do the same with a union,
right?

Rune


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Sriram Chandrasekaran" <sriram@ulticom.com>
Date: Thu, 18 Apr 2002 07:26:49 GMT
Raw View
"chris" <chris@bubblescope.net> wrote in message
news:a9ich6$g9l$1@pegasus.csx.cam.ac.uk...
> Hello!
>
> After my earlier thoughts about a "static class", I've realised that what
I
> really want in "comp. sci. terms" is a way of defining a class as a
> singleton. Doing so allows extra optimisations to be performed on the
> class...
---------------Singleton is a design pattern.
>
> I can see two ways of doing this:
>
> 1) define "singleton" as a protected word and allow "singleton class"
>
> 2) allow a section of a namespace to be "private", and using a namespace
as
> a singleton in a quite nice looking manner.
>
> So, two questions:
>
> 1) how likely is it to be able to add "singleton class", and how likely
> would it be anyone else would use it?
----------- Many design patterns are most frequently used in all projects.
> 2) It seems a little strange to me that you can't define private members
of
> a namespace. Why is that? Could it be changed?
------------Is namespace class-like or just a convention to avoid confusion
?

> Thank you.
>
> Chris
>
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: xleobx@qmailcomq.com
Date: Thu, 18 Apr 2002 07:28:23 GMT
Raw View
chris <chris@bubblescope.net> wrote:
> Hello!

> After my earlier thoughts about a "static class", I've realised that what I
> really want in "comp. sci. terms" is a way of defining a class as a
> singleton. Doing so allows extra optimisations to be performed on the
> class...

> I can see two ways of doing this:

> 1) define "singleton" as a protected word and allow "singleton class"

What should the compiler do when it sees "singleton class"? More specifically,
what exactly should it do that you cannot currently do using the existing syntax?

> 2) allow a section of a namespace to be "private", and using a namespace as
> a singleton in a quite nice looking manner.

I've never heard of a namespace constructor, to start with.

 Leo

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Sriram Chandrasekaran" <sriram@ulticom.com>
Date: Thu, 18 Apr 2002 07:27:59 GMT
Raw View
"chris" <chris@bubblescope.net> wrote in message
news:a9ich6$g9l$1@pegasus.csx.cam.ac.uk...
> Hello!
>
> After my earlier thoughts about a "static class", I've realised that what
I
> really want in "comp. sci. terms" is a way of defining a class as a
> singleton. Doing so allows extra optimisations to be performed on the
> class...
>
> I can see two ways of doing this:
>
> 1) define "singleton" as a protected word and allow "singleton class"
>
> 2) allow a section of a namespace to be "private", and using a namespace
as
> a singleton in a quite nice looking manner.
>
> So, two questions:
>
> 1) how likely is it to be able to add "singleton class", and how likely
> would it be anyone else would use it?
>>> A singleton is a pattern , almost used from application's perspective so
it cannot or may not be useful
in all circumstances.
> 2) It seems a little strange to me that you can't define private members
of
> a namespace. Why is that? Could it be changed?
> >>> I have a small clarification . Can namespace have an instance ?
> Thank you.
>
> Chris
>
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "chris" <chris@bubblescope.net>
Date: Thu, 18 Apr 2002 20:21:04 GMT
Raw View
> > I can see two ways of doing this:
>
> > 1) define "singleton" as a protected word and allow "singleton class"
>
> What should the compiler do when it sees "singleton class"? More
specifically,
> what exactly should it do that you cannot currently do using the existing
syntax?
>

The main thing that you cannot do with the existing syntax is optimise..

for example, in C I often wrote things like:
/*class player definition*/
int player_x;
int player_y;
void player_set_xy(int ix,int iy);
void player_move(void);
/*end class definition*/

I would like to be able to (in the fairly obvious sense) turn that into C++
code, which is just as fast as the originial C code when compiled. However,
were I do to the obvious thing:

void player_classdef { ... /* define player here */ };

player_classdef player;

then the function calls player_set_xy and player_move would have a "hidden
pointer" to "player" (this is minimal, but still a slowdown).

Also (not so obvious in this example), sometimes it is important that no
more than one copy of an object is ever created, as it wouldn't make sense.
In these cases it would be nice to be able to somehow put into the language
that there should be but one copy.

Where this built into the standard, I'm not 100% sure what the best thing to
do with regard to constructors, etc. I just think that having looked through
texts and found enough mentions of how to construct a singleton class, it
would be useful to have it in the standard, where we could do (a)
compile-time checking, and (b) optimisations


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: xleobx@qmailcomq.com
Date: Fri, 19 Apr 2002 01:05:29 GMT
Raw View
chris <chris@bubblescope.net> wrote:
>> > I can see two ways of doing this:
>>
>> > 1) define "singleton" as a protected word and allow "singleton class"
>>
>> What should the compiler do when it sees "singleton class"? More
> specifically,
>> what exactly should it do that you cannot currently do using the existing
> syntax?
>>

> The main thing that you cannot do with the existing syntax is optimise..

Use static class members and static member functions to avoid passing
a "hidden pointer".

> Also (not so obvious in this example), sometimes it is important that no
> more than one copy of an object is ever created, as it wouldn't make sense.

By "copy", do you really mean "instance"?

> In these cases it would be nice to be able to somehow put into the language
> that there should be but one copy.

Ok. Consider this:

header.h:
singleton class C { ... };
void foo(), bar(), baz();

file1.cc:
#include "header.h"
C c1;
....

void bar() {
 ...
 baz();
}

file2.cc:

#include "header.h"
void foo() {
 C c2;
 ...
 bar();
}

void baz() {
 C c3;
}

When should the error be diagnozed, if file1.cc and file2.cc are
compiled separately, I wonder? It is impractical to diagnoze during
compile time, and nothing prevents you from writing

class C {
static bool ctor_done;
....
};

bool C::ctor_done = false;
C::C() {
 if (ctor_done) abort();
 ctor_done = true;
....
}

> Where this built into the standard, I'm not 100% sure what the best thing to
> do with regard to constructors, etc. I just think that having looked through
> texts and found enough mentions of how to construct a singleton class, it
> would be useful to have it in the standard, where we could do (a)
> compile-time checking, and (b) optimisations

Compile-time checking is useless at best, optimizations are easily enabled
by using static member functions and static data members, and runtime checking
is easily done by hand (although it becomes tedious when there are
many constructors). So the only use I can see is in simplifying
runtime checking, but I do not believe that the standard committee
is eager enough to add syntactical sugar to the language to consider
that proposal.

 Leo

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "chris" <chris@bubblescope.net>
Date: Wed, 17 Apr 2002 16:59:25 GMT
Raw View
Hello!

After my earlier thoughts about a "static class", I've realised that what I
really want in "comp. sci. terms" is a way of defining a class as a
singleton. Doing so allows extra optimisations to be performed on the
class...

I can see two ways of doing this:

1) define "singleton" as a protected word and allow "singleton class"

2) allow a section of a namespace to be "private", and using a namespace as
a singleton in a quite nice looking manner.

So, two questions:

1) how likely is it to be able to add "singleton class", and how likely
would it be anyone else would use it?

2) It seems a little strange to me that you can't define private members of
a namespace. Why is that? Could it be changed?

Thank you.

Chris


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Ludwig Pumberger" <pumberger@direkt.at>
Date: Wed, 17 Apr 2002 15:39:37 CST
Raw View
> 2) allow a section of a namespace to be "private", and using a namespace
as
> a singleton in a quite nice looking manner.
>

A namespace can't act as a real singleton because it is no object. i.e a
namespace doesn't have an adress, you can't derive from it, and everyone can
reopen it.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]