Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Singleton pattern with MinGW
Forum Updated to NodeBB v4.3 + New Features

Singleton pattern with MinGW

Scheduled Pinned Locked Moved Solved General and Desktop
mingw64singleton
5 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jendker
    wrote on 7 Dec 2018, 14:31 last edited by Jendker 12 Jul 2018, 15:33
    #1

    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]
       Singleton::create.store(create);
       ^~~~~~~~~
    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.

    K 1 Reply Last reply 8 Dec 2018, 00:37
    0
    • J Jendker
      7 Dec 2018, 14:31

      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]
         Singleton::create.store(create);
         ^~~~~~~~~
      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.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 8 Dec 2018, 00:37 last edited by kshegunov 12 Aug 2018, 00:38
      #2

      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.

      Read and abide by the Qt Code of Conduct

      J 1 Reply Last reply 8 Dec 2018, 18:45
      2
      • K kshegunov
        8 Dec 2018, 00:37

        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.

        J Offline
        J Offline
        Jendker
        wrote on 8 Dec 2018, 18:45 last edited by
        #3

        @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 :)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Dec 2018, 20:14 last edited by
          #4

          Hi,

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 10 Dec 2018, 09:43
          2
          • S SGaist
            8 Dec 2018, 20:14

            Hi,

            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.

            J Offline
            J Offline
            Jendker
            wrote on 10 Dec 2018, 09:43 last edited by
            #5

            @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 :)

            1 Reply Last reply
            0

            1/5

            7 Dec 2018, 14:31

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved