Singleton Pattern with struct

Era Scarecrow rtcvb32 at yahoo.com
Thu Jan 24 10:28:00 PST 2013


On Thursday, 24 January 2013 at 16:49:53 UTC, Maxim Fomin wrote:
> On Thursday, 24 January 2013 at 16:17:34 UTC, Era Scarecrow 
> wrote:
>> Hmmm. You could separate the data and remove the pointer... 
>> then use alias this.
>>
>> [code]
>>  struct Singleton {
>>    static SingletonData single;
>>    alias single this;
>>    @disable this(this);
>>    //disable copy/assignment? Not that it would matter...
>>    //copying nothing does nothing, as there's nothing here.
>>
>>    private static struct SingletonData {
>>      //methods and data here.
>>    }
>> [/code]
>
> I agree, but disabling ctors makes creating module-level object 
> tricky. Static struct initialization might help.

  I'll have to agree. But in that case don't disable the default 
ctor (not that the Singleton has any contents anyways); Actually 
opAssign/postblit only needs to be disabled inside SingletonData. 
I'd think that would work.

[code]
   struct Singleton {
     static SingletonData single;
     alias single this;

     private static struct SingletonData {
       @disable void opAssign();
       //methods and data here.
     }
   }
[/code]


More information about the Digitalmars-d-learn mailing list