Singleton Pattern with struct

Artur Skawina art.08.09 at gmail.com
Thu Jan 24 09:29:12 PST 2013


On 01/24/13 18:14, Artur Skawina wrote:
> struct Singleton  {
> private:
>     this( int a = 0 ) {} ;
>     static Singleton* s ;
> 
> public:
>     @disable this();
>     @disable this(this);
>     static instance() @property {
>         static struct Ref(T) { T* obj; ref g() @property { return *obj; } alias obj this; @disable this(this); }
>         if ( s is null )
>             s = new Singleton( 0 ) ;
>         return Ref!(typeof(this))(s) ;
>     }
>     
>     int val = 0 ;
> }

Grr. What I meant to write was of course:

 static struct Ref(T) { T* obj; ref g() @property { return *obj; } alias g this; @disable this(this); }

Sorry,

artur


More information about the Digitalmars-d-learn mailing list