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. QList uses move constructor?

QList uses move constructor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlistqt6
3 Posts 2 Posters 917 Views
  • 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.
  • C Offline
    C Offline
    CJha
    wrote on 25 May 2022, 12:53 last edited by CJha
    #1

    Hi, I am using Qt6.3 and I am trying to store an object in a QList. My object provides the default constructor, copy constructor, copy assignment operator and destructor but I have deleted the move constructor and move assignment operator.

    class Base
    {
    public:
        Base(Base&& other) = delete; // deleted move constructor
        Base& operator=(Base&& other) = delete; // deleted move assignment operator
    
        Base(); // default constructor
        Base(const Base& other); // copy constructor
        Base& operator=(const Base& other); // copy assignment operator
        ~Base(); // destructor
    
        int value{0};
    };
    
    QDataStream& operator<<(QDataStream& out, const Base& obj);
    QDataStream& operator>>(QDataStream& in, Base& obj);
    
    Q_DECLARE_METATYPE(Base)
    

    In my cpp file:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        qRegisterMetaType<Base>();
        Base base;
        QList<Base> baseList;
        baseList << base; // produces error C2280
    }
    

    When I try to store my object in a QList I get an error: C2280 'Base::Base(Base &&)': attempting to reference a deleted function

    It is trying to access the move constructor when I am trying to store base in my baseList, which is deleted and so I am getting the error. When I comment out the deleted move constructor then it works.

    According to Qt's documentation from Container Classes webpage:

    The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a copy constructor, and an assignment operator. For some operations a default constructor is also required.

    And from the QList webpage:

    QList's value type must be an assignable data type.

    In Qt6 QList has the same implementation as QVector in Qt5:

    Prior to Qt 6, QVector and QList were separate classes. In Qt 6, they are unified: Qt 5 QList implementation is gone and both classes use updated QVector implementation instead. QList is the class with the actual implementation and QVector is an alias (typedef) to QList.

    The QVector in Qt5 used to use copy constructor to store data. So, why is it trying to access the move constructor in the first place? And is there any way to avoid it without writing an explicit move constructor?

    J 1 Reply Last reply 25 May 2022, 13:02
    0
    • C CJha
      25 May 2022, 12:53

      Hi, I am using Qt6.3 and I am trying to store an object in a QList. My object provides the default constructor, copy constructor, copy assignment operator and destructor but I have deleted the move constructor and move assignment operator.

      class Base
      {
      public:
          Base(Base&& other) = delete; // deleted move constructor
          Base& operator=(Base&& other) = delete; // deleted move assignment operator
      
          Base(); // default constructor
          Base(const Base& other); // copy constructor
          Base& operator=(const Base& other); // copy assignment operator
          ~Base(); // destructor
      
          int value{0};
      };
      
      QDataStream& operator<<(QDataStream& out, const Base& obj);
      QDataStream& operator>>(QDataStream& in, Base& obj);
      
      Q_DECLARE_METATYPE(Base)
      

      In my cpp file:

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          ui.setupUi(this);
          qRegisterMetaType<Base>();
          Base base;
          QList<Base> baseList;
          baseList << base; // produces error C2280
      }
      

      When I try to store my object in a QList I get an error: C2280 'Base::Base(Base &&)': attempting to reference a deleted function

      It is trying to access the move constructor when I am trying to store base in my baseList, which is deleted and so I am getting the error. When I comment out the deleted move constructor then it works.

      According to Qt's documentation from Container Classes webpage:

      The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a copy constructor, and an assignment operator. For some operations a default constructor is also required.

      And from the QList webpage:

      QList's value type must be an assignable data type.

      In Qt6 QList has the same implementation as QVector in Qt5:

      Prior to Qt 6, QVector and QList were separate classes. In Qt 6, they are unified: Qt 5 QList implementation is gone and both classes use updated QVector implementation instead. QList is the class with the actual implementation and QVector is an alias (typedef) to QList.

      The QVector in Qt5 used to use copy constructor to store data. So, why is it trying to access the move constructor in the first place? And is there any way to avoid it without writing an explicit move constructor?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 25 May 2022, 13:02 last edited by
      #2

      @CJha it is ?

      because I ran into the issue, that QList insisted to use the copy assignment operator, no matter what.

      https://bugreports.qt.io/browse/QTBUG-101432

      that said,

      Your class has a user declared copy constructor, so simply don't declare the move assignment at all, then no implicit move assignment operator will be created and the copy assignment should be selected.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      C 1 Reply Last reply 25 May 2022, 13:21
      2
      • J J.Hilk
        25 May 2022, 13:02

        @CJha it is ?

        because I ran into the issue, that QList insisted to use the copy assignment operator, no matter what.

        https://bugreports.qt.io/browse/QTBUG-101432

        that said,

        Your class has a user declared copy constructor, so simply don't declare the move assignment at all, then no implicit move assignment operator will be created and the copy assignment should be selected.

        C Offline
        C Offline
        CJha
        wrote on 25 May 2022, 13:21 last edited by
        #3

        @J-Hilk Thanks! Ok, so removing the deleted move constructor and move assignment operator works. I was marking it delete because I wanted to avoid implicitly generated move semantics, but now I understand that it won't be generated by itself if I have a copy constructor already.

        It is still weird that QList was giving error for deleted move semantics when it is not using move semantics at all.

        1 Reply Last reply
        1

        2/3

        25 May 2022, 13:02

        • Login

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