lundi 4 septembre 2023

Singleton design pattern - optimized version

This following version of singleton design pattern is the most efficient one. (Benchmark)

class SomeClass {
public: /** Singleton **/
    static SomeClass& instance() {
        return singleInstance;
    };
private: 
    static SomeClass singleInstance;
    SomeClass() = default;
    SomeClass(const SomeClass&) = delete;
    SomeClass& operator=(const SomeClass&) = delete;
};
SomeClass SomeClass::singleInstance;

int main()
{
    // stuff here
}

What are the distinctions between this version and the one utilizing raw pointers (i.e., static SomeClass*)?

Does this version operate reliably in all scenarios? If it does, why isn't it more widely discussed in blogs and references?

If it doesn't, what potential issues could it lead to?

Aucun commentaire:

Enregistrer un commentaire