On Tuesday, 9 April 2019 at 14:25:18 UTC, Mike Parker wrote:

Off the top of my head, to get a Singleton template, you could implement all of your singleton plumbing (thread safety if you need it, etc) in the template and add a `static _instance` member just as you would for any non-templated singleton:

class Singleton(T) {
    private static Singleton!T _instance;
    static Singleton!T instance() {
        if(_instance is null) {
            _instance = new Singleton!T;
        }
        ...
    }

    private T _thing;
    ...
}

And you can then instantiate directly or, more conveniently, use an alias:

alias Accelerators = Singleton!AccelGroup;

Then:

Accelerators.instance.doSomething();

So, I guess the short answer is 'no.' A template can't really substitute for a singleton without actually becoming a singleton in and of itself.

I'm still struggling to understand templates, but I'll keep at it.

Thanks, Mike.


Reply via email to