Singleton pattern with MinGW

  • Hello everyone,

    I am right now switching from MSVC to MinGW and here proposed code for singleton does not work anymore producing following error in line 30 of singleton.h:

    singleton.h:30: error: invalid conversion from 'Singleton<Database>::CreateInstanceFunction {aka Database* (*)()}' to 'QBasicAtomicPointer<void>::Type {aka void*}' [-fpermissive];
    qbasicatomic.h:240: note:   initializing argument 1 of 'void QBasicAtomicPointer<X>::store(QBasicAtomicPointer<X>::Type) [with X = void; QBasicAtomicPointer<X>::Type = void*]'
         void store(Type newValue) Q_DECL_NOTHROW { Ops::store(_q_value, newValue); }

    What would be the way to go around it with MinGW?

    EDIT: Maybe I am just creating the instance not properly to make it work with MinGW?
    I am calling it like this (creating instance of class Database):

    static inline Database* instance() { return singleton<Database>::instance(Database::createInstance); }

    this is my "interpretation" because the example in main of the example:

    // main.cpp
    #include <QTextStream>
    #include "myclass.h"
    #define MyClassInstance Singleton<MyClass>::instance()
    int main(int argc, char* argv[])
        QTextStream(stdout) << MyClass::instance()->metaObject()->className() << endl;
        return 0;

    was not clear to me.

  • Moderators

    The function pointer has be cast to a data pointer, so that should be done manually, which it isn't.
    But for the love of god, don't use that $%^@.

    If you're so intent on having something globally accessible, just use Q_GLOBAL_STATIC instead.

  • @kshegunov Thanks that solved it!

    Global is very useful in my case, previously I was passing the Database object from main through all the classes to have access to it whenever possible. This makes it much more reasonable. But I understand, that globals are generally evil :)

  • Lifetime Qt Champion


    If you are talking about a QSqlDatabase object then what you are doing is wrong. There's no need for any global object of that class as show in the details of the class.

  • @SGaist Thank you, but this is actually the class where I am creating the tables when database is empty and holding all the models and necessary functions to work on them. So it is fine in this case :)

Log in to reply