Is a template like copy-paste?

Ary Manzana asterite at gmail.com
Fri Oct 27 05:29:12 PDT 2006


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.



More information about the Digitalmars-d-bugs mailing list