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. "Don't call QList::front() on a temporary" clazy warning, what is it about?
Forum Updated to NodeBB v4.3 + New Features

"Don't call QList::front() on a temporary" clazy warning, what is it about?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.4k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by Violet Giraffe
    #1

    The following code seems perfectly valid to me, but produces a clazy warning:

    openLogFile(mimeData->urls().front().toLocalFile());
    

    The warning is "don't call QList::front() on a temporary". I don't understand it, what's the problem? Is there actually a problem?

    I'm pretty sure that all the temporaries here have the correct life span for this to work properly, and eventually the call chain results in a QString that's passed to the function as argument.

    P. S. The QList::front() method is not even ref-qualified. If it was invalid for temporaries they could have &-qualified it, or provided a && overload that is valid.
    P. P. S. The front() method returns a non-const reference (as it should), which would be an error if I tried to modify it, but if that's what it is about then the wording of the warning is confusing.

    Christian EhrlicherC 1 Reply Last reply
    0
    • V Violet Giraffe

      @Christian-Ehrlicher A deep copy of the whole container? Or just the single front element?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:

      A deep copy of the whole container?

      ... the container needs to be detached and a deep copy might be generated.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • V Violet Giraffe

        The following code seems perfectly valid to me, but produces a clazy warning:

        openLogFile(mimeData->urls().front().toLocalFile());
        

        The warning is "don't call QList::front() on a temporary". I don't understand it, what's the problem? Is there actually a problem?

        I'm pretty sure that all the temporaries here have the correct life span for this to work properly, and eventually the call chain results in a QString that's passed to the function as argument.

        P. S. The QList::front() method is not even ref-qualified. If it was invalid for temporaries they could have &-qualified it, or provided a && overload that is valid.
        P. P. S. The front() method returns a non-const reference (as it should), which would be an error if I tried to modify it, but if that's what it is about then the wording of the warning is confusing.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        mimeData->urls() returns an object so it's a temporary. A temporary is by default non-const so since you're calling (the non-const) front() on it, the container needs to be detached and a deep copy might be generated. And that's what the warning is about.
        Use e.g. constFirst() or store the return of mimeData->urls() in a local variable first.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        V 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          mimeData->urls() returns an object so it's a temporary. A temporary is by default non-const so since you're calling (the non-const) front() on it, the container needs to be detached and a deep copy might be generated. And that's what the warning is about.
          Use e.g. constFirst() or store the return of mimeData->urls() in a local variable first.

          V Offline
          V Offline
          Violet Giraffe
          wrote on last edited by
          #3

          @Christian-Ehrlicher A deep copy of the whole container? Or just the single front element?

          Christian EhrlicherC 1 Reply Last reply
          0
          • V Violet Giraffe

            @Christian-Ehrlicher A deep copy of the whole container? Or just the single front element?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:

            A deep copy of the whole container?

            ... the container needs to be detached and a deep copy might be generated.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • V Offline
              V Offline
              Violet Giraffe
              wrote on last edited by
              #5

              Thanks for the explanation. That's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer std facilities over Qt and do not use Qt classes in performance-critical code.

              Christian EhrlicherC 1 Reply Last reply
              1
              • V Violet Giraffe has marked this topic as solved on
              • V Violet Giraffe

                Thanks for the explanation. That's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer std facilities over Qt and do not use Qt classes in performance-critical code.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:

                hat's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer std facilities over Qt and do not use Qt classes in performance-critical code.

                I don't understand what you're trying to tell us here.

                What's odd? CopyOnWrite is a design decission by Qt and you have to take care for it.

                With plain std you have the problem that the whole container would be copied anyway (forgetting a possible rvo).

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                V 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:

                  hat's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer std facilities over Qt and do not use Qt classes in performance-critical code.

                  I don't understand what you're trying to tell us here.

                  What's odd? CopyOnWrite is a design decission by Qt and you have to take care for it.

                  With plain std you have the problem that the whole container would be copied anyway (forgetting a possible rvo).

                  V Offline
                  V Offline
                  Violet Giraffe
                  wrote on last edited by Violet Giraffe
                  #7

                  @Christian-Ehrlicher, calling front() can never create a copy of an std container. But I finally understood just now why it does in Qt.

                  Christian EhrlicherC 1 Reply Last reply
                  1
                  • V Violet Giraffe

                    @Christian-Ehrlicher, calling front() can never create a copy of an std container. But I finally understood just now why it does in Qt.

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:

                    calling front() can never create a copy of an std container.

                    Yes because the copy occoured already before.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    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