Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-119912

moc: Generated code calls the overloaded non-const getter for when accessing the property

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • dev
    • Build tools: moc
    • None

      The following code cases the call of int a() but not the int a() const as could expected.

      class MyClass {
          Q_OBJECT
          Q_PROPERTY(int a READ a)
      
          int a() const
          {
               qDebug() << "const";
               return m_a;
          }
          int &a()
          {
               qDebug() << "non-const";
               return m_a;
          }
      private:
          int m_a  = 1;
      };
      

      This happens because the reader code accesses the MyClass uses pointer to non-const data when reading values:

      if (_c == QMetaObject::ReadProperty) {
              auto *_t = reinterpret_cast<MyClass *>(_o);
              (void)_t;
              void *_v = _a[0];
              switch (_id) {
              case 0: *reinterpret_cast< int*>(_v) = _t->a(); break;
              default: break;
              }
      

      It would be nice to detect this situation and force calling const function instead, since mutable getters might lead to really expensive shared data detach.

        For Gerrit Dashboard: QTBUG-119912
        # Subject Branch Project Status CR V

            fabiankosmale Fabian Kosmale
            semlanik Alexey Edelev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:

                There is 1 open Gerrit change