Is a template like copy-paste?

Bruno Medeiros brunodomedeiros+spam at com.gmail
Tue Oct 31 04:58:33 PST 2006


Ary Manzana wrote:
> I have a template for singleton:
> 
> ---
> template Singleton() {
> 
>    private static typeof(this) _instance;
>    private this() { }
> 
>    public static typeof(this) instance() {
>        if (!_instance) {
>            _instance = new typeof(this)();
>        }
>        return _instance;
>    }
> 
> }
> ---
> 
> I use it like this:
> 
> ---
> class Bla { // Line 15
> 
>    mixin Singleton!();
> 
> }
> 
> void main() {
>     Bla bla = Bla.instance;
> }
> ---
> 
> The compiler says:
> main.d(15): class main.Bla main.Bla.Singleton!().this is private
> 
> Then I copy-paste the template into the class definition, to see what 
> happens:
> 
> ---
> class Bla {
> 
>    private static typeof(this) _instance;
>    private this() { }
> 
>    public static typeof(this) instance() {
>        if (!_instance) {
>            _instance = new typeof(this)();
>        }
>        return _instance;
>    }
> 
> }
> 
> void main() {
>     Bla bla = Bla.instance;
> }
> ---
> 
> Compiles fine.
> 
> If I change "private this() { }" to "protected this() { }" in the 
> singleton template, both compile fine. However, this is just a 
> workaround, the first should also work.

Consider: http://d.puremagic.com/bugzilla/show_bug.cgi?id=49
(that bug reported is outdated though, it needs to be updated)

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d-bugs mailing list