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

Cannot create const shared pointers for classes derived from QEnableSharedFromThis

    XMLWordPrintable

Details

    • 401507b348936d346cec8734cac5cacad70a3174

    Description

      This simple piece of code:

      #include <QEnableSharedFromThis>
      
      class MyClass : public QEnableSharedFromThis<MyClass> { };
      
      int main()
      {
          QSharedPointer<const MyClass> myObject(new MyClass);
      }
      

      causes gcc to say (clang is similar):

      In file included from qtbase/include/QtCore/qsharedpointer_impl.h:1:0,
                       from qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer.h:42,
                       from qtbase/include/QtCore/qsharedpointer.h:1,
                       from qtbase/include/QtCore/QEnableSharedFromThis:1,
                       from /tmp/sharedfromthis/main.cpp:1:
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h: In instantiation of ‘QtNamespace::QWeakPointer<T>& QtNamespace::QWeakPointer<T>::operator=(const QtNamespace::QSharedPointer<X>&) [with X = const MyClass; T = MyClass]’:
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:749:21:   required from ‘void QtNamespace::QEnableSharedFromThis<T>::initializeFromSharedPointer(const QtNamespace::QSharedPointer<X>&) const [with X = const MyClass; T = MyClass]’
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:502:9:   required from ‘void QtNamespace::QSharedPointer<T>::enableSharedFromThis(const QtNamespace::QEnableSharedFromThis<X>*) [with X = MyClass; T = const MyClass]’
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:527:29:   required from ‘void QtNamespace::QSharedPointer<T>::internalConstruct(T*, Deleter) [with Deleter = QtNamespace::QtSharedPointer::NormalDeleter; T = const MyClass]’
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:312:24:   required from ‘QtNamespace::QSharedPointer<T>::QSharedPointer(T*) [with T = const MyClass]’
      /tmp/sharedfromthis/main.cpp:7:55:   required from here
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:81:35: error: no matching function for call to ‘qt_sharedpointer_cast_check(const MyClass*)’
           qt_sharedpointer_cast_check<T>(static_cast<X *>(0))
                                         ^
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:667:9: note: in expansion of macro ‘QSHAREDPOINTER_VERIFY_AUTO_CAST’
               QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X); // if you get an error in this line, the cast is invalid
               ^
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:79:34: note: candidate: template<class T> void QtNamespace::qt_sharedpointer_cast_check(T*)
       template<typename T> inline void qt_sharedpointer_cast_check(T *) { }
                                        ^
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:79:34: note:   template argument deduction/substitution failed:
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:81:35: note:   cannot convert ‘0u’ (type ‘const MyClass*’) to type ‘MyClass*’
           qt_sharedpointer_cast_check<T>(static_cast<X *>(0))
                                         ^
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:667:9: note: in expansion of macro ‘QSHAREDPOINTER_VERIFY_AUTO_CAST’
               QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X); // if you get an error in this line, the cast is invalid
               ^
      In file included from qtbase/include/QtCore/qsharedpointer_impl.h:1:0,
                       from qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer.h:42,
                       from qtbase/include/QtCore/qsharedpointer.h:1,
                       from qtbase/include/QtCore/QEnableSharedFromThis:1,
                       from /tmp/sharedfromthis/main.cpp:1:
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:668:20: error: invalid conversion from ‘const MyClass*’ to ‘MyClass*’ [-fpermissive]
               internalSet(o.d, o.data());
                          ^
      qtbase/include/QtCore/../../../../src/qtbase/src/corelib/tools/qsharedpointer_impl.h:709:17: note:   initializing argument 2 of ‘void QtNamespace::QWeakPointer<T>::internalSet(QtNamespace::QWeakPointer<T>::Data*, T*) [with T = MyClass; QtNamespace::QWeakPointer<T>::Data = QtNamespace::QtSharedPointer::ExternalRefCountData]’
           inline void internalSet(Data *o, T *actual)
                       ^
      

      Equivalent code with std::enable_shared_from_this works fine.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              thiago Thiago Macieira
              kandeler Christian Kandeler
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes