Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Why foreach is not using const reference (it is making copies)?

Why foreach is not using const reference (it is making copies)?

Scheduled Pinned Locked Moved C++ Gurus
qt5qtcore
7 Posts 2 Posters 3.8k 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.
  • P Offline
    P Offline
    Prog
    wrote on last edited by
    #1

    Why QForeachContainer is using variable c defined as "const T c;" and not as "const T &c;"?
    I would expect that this means that during constructing of QForeachContainer in every foreach statement original container t of type T is copied from it (not const-referenced to it) with statement "c(t)" in the constructor of QForeachContainer.
    Why there is necessity in this overhead?

    Code from qglobal.h:

    template <typename T>
      class QForeachContainer {
        QForeachContainer &operator=(const QForeachContainer &) Q_DECL_EQ_DELETE;
    public:
        inline QForeachContainer(const T& t) : c(t), i(c.begin()), e(c.end()), control(1) { }
        const T c;
        typename T::const_iterator i, e;
        int control;
    };
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      If you want to avoid the copy, you can use:

      foreach  (const QString &str, myStringList) {
          qDebug() << str;
      }
      

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

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        If you want to avoid the copy, you can use:

        foreach  (const QString &str, myStringList) {
            qDebug() << str;
        }
        
        P Offline
        P Offline
        Prog
        wrote on last edited by Prog
        #3

        @SGaist Mmm ... I was asking about copying of container object (which is myStringList in your example), not about its items. Did you get the question correctly?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          My bad, indeed, I misunderstood your question. The answer you are looking for is here in Qt's documentation

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

          P 1 Reply Last reply
          1
          • SGaistS SGaist

            My bad, indeed, I misunderstood your question. The answer you are looking for is here in Qt's documentation

            P Offline
            P Offline
            Prog
            wrote on last edited by
            #5

            @SGaist Mmm ... documentation states that copying happens but I did not get Why this is done like that?
            I can not imagine good cases of using this container-copy feature of foreach ... to be able to modify container while looping through its hiddenly copied duplicate ... well ... this is something ... let's say special ;)

            May be such implementation of iterator-loop have the right to exist but in this case I would expect it to co-exist with another iterator-loop which works in more straightforward for developers way (with just const-ref to container).

            P 1 Reply Last reply
            0
            • P Prog

              @SGaist Mmm ... documentation states that copying happens but I did not get Why this is done like that?
              I can not imagine good cases of using this container-copy feature of foreach ... to be able to modify container while looping through its hiddenly copied duplicate ... well ... this is something ... let's say special ;)

              May be such implementation of iterator-loop have the right to exist but in this case I would expect it to co-exist with another iterator-loop which works in more straightforward for developers way (with just const-ref to container).

              P Offline
              P Offline
              Prog
              wrote on last edited by
              #6

              Moreover Qt-containers tries to be stl-compatible and If Qt-developer is used to use foreach for Qt-containers it could be non-obvious for him/her that with stl-container the container data copying is happening every time foreach statement is used (as soon as stl-containers does not do implicit sharing)

              For me it is like very hidden trap.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                If you would like to discuss that issue further down, then I'd recommend asking it on the interest mailing list You'll find there Qt's developers/maintainers (this forum is mor e user oriented)

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

                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