Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. QList::emplace_back()
Forum Updated to NodeBB v4.3 + New Features

QList::emplace_back()

Scheduled Pinned Locked Moved Unsolved Qt 6
4 Posts 2 Posters 355 Views 1 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.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #1

    I'm finally moving our projects to the Qt6 space. Sadly this means my beloved QVector is no longer with us, only in name.

    I stumbled on the following (simplified example):

    #include <QCoreApplication>
    #include <QList>
    #include <QDebug>
    
    class TestClassConst{
    public:
        TestClassConst(const int &value) : m_value(value){
            qDebug() << "Constructor";
        }
    
        TestClassConst(TestClassConst && other): m_value{other.m_value}
        {
            qDebug() << "Move Constructor";
        }
    
        TestClassConst(const TestClassConst & other): m_value{other.m_value}
        {
            qDebug() << "Copy Constructor";
        }
        
        // QList::emplace_back requieres this! delete is not possible
        // TestClassConst & operator=(const TestClassConst & other) = delete;
        TestClassConst & operator=(const TestClassConst & other)
        {
            qDebug() << "Copy Assignment";
            return *this;
        }
    
        int value() const {return m_value;}
    
    private:
        const int m_value{0};
    };
    
    int main(int argc, char *argv[])
    {
       QCoreApplication app(argc, argv);
    
       qDebug() << "std";
       //the truth
       std::vector<TestClassConst> vectorStd;
       vectorStd.emplace_back(42);
    
       qDebug() << "Qt";
       //the lie
       QList<TestClassConst> list;
       list.emplace_back(41);
       qDebug() << list.first().value();
    }
    

    the console output:

    std
    Constructor
    Qt
    Constructor
    Move Constructor
    41
    

    however, the QList section will not compile if I set the Copy Assignment operator to delete. Even though QList::emplace_back doesn't even seem to use it. I'm super confused and I would rather not have an empty non functional copy assignment operator in my classes if I can avoid it.

    Any help or enlightenment appreciated!


    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Shankarlinga M
      wrote on last edited by
      #2

      If you really want to enforce immutability (no copy assignment), you can:

      Use std::vector<TestClassConst> instead of QList<TestClassConst>.
      If you must use QList, consider allowing move assignment but still deleting copy assignment:

      TestClassConst & operator=(const TestClassConst & other) = delete;
      TestClassConst & operator=(TestClassConst && other) noexcept = default;

      This will prevent copies but allow moves, making it compatible with QList.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shankarlinga M
        wrote on last edited by
        #3

        QList, despite its improvements in Qt 6, still has legacy behavior that requires an assignable type in some cases. If you're working with immutable or non-copyable types, std::vector is usually the better choice.

        J.HilkJ 1 Reply Last reply
        0
        • S Shankarlinga M

          QList, despite its improvements in Qt 6, still has legacy behavior that requires an assignable type in some cases. If you're working with immutable or non-copyable types, std::vector is usually the better choice.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Shankarlinga-M thanks for your input!

          The use case is basically a container class, with - almost only -, readOnly access from outside.

          It will be read from user provided files during runtime and exist until a new set is read or the program is closed.

          The objects are created and "stored" in a List/Vector to be passed to where they are need to be read.
          So the list should per design choice be immutable. And I would like to enforce it.

          The default move assignment operator is deleted due to const members, so thats out too.

          Looks so far, as if the cleanest way would be using std::vector. Not sure how much refactor/extra work that will be :(


          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.

          1 Reply Last reply
          0

          • Login

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