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. Using a const QString & if a function returns a QString
Forum Updated to NodeBB v4.3 + New Features

Using a const QString & if a function returns a QString

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 3.8k Views 2 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.
  • L Offline
    L Offline
    l3u_
    wrote on 8 May 2019, 19:13 last edited by
    #1

    Hi :-)

    Maybe this is a dumb question (maybe caused by lack of C++ knowledge), but I'm not sure if I have faulty reasoning about this:

    When calling a function using e. g. a QString, it seems to be reasonable to me to use (if possible) a const QString & to call it so that no copy of the QString is passed to the function, but only a reference to the actual data. But what about a function returning a QString?

    I have e. g. the following code:

    const QString text = jsonObject.value(someKey).toString();
    someFunction(text);
    someOtherFunction(text);
    

    The following works as well:

    const QString &text = jsonObject.value(someKey).toString();
    someFunction(text);
    someOtherFunction(text);
    

    QJsonValue::toString() returns a QString. So what is referenced in the second case? The return value of the function as long as it doesn't go out of scope? And is it better to do it in this way?

    Thanks for all clarification!

    A J 2 Replies Last reply 8 May 2019, 19:18
    1
    • L l3u_
      8 May 2019, 19:13

      Hi :-)

      Maybe this is a dumb question (maybe caused by lack of C++ knowledge), but I'm not sure if I have faulty reasoning about this:

      When calling a function using e. g. a QString, it seems to be reasonable to me to use (if possible) a const QString & to call it so that no copy of the QString is passed to the function, but only a reference to the actual data. But what about a function returning a QString?

      I have e. g. the following code:

      const QString text = jsonObject.value(someKey).toString();
      someFunction(text);
      someOtherFunction(text);
      

      The following works as well:

      const QString &text = jsonObject.value(someKey).toString();
      someFunction(text);
      someOtherFunction(text);
      

      QJsonValue::toString() returns a QString. So what is referenced in the second case? The return value of the function as long as it doesn't go out of scope? And is it better to do it in this way?

      Thanks for all clarification!

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 8 May 2019, 19:18 last edited by
      #2

      Hi @l3u_

      what you found is Reference Lifetime Extension - so you referenced a QString.

      However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often (and that is used within Qt itself also).

      Regards

      Qt has to stay free or it will die.

      B 1 Reply Last reply 7 Nov 2020, 15:10
      4
      • L Offline
        L Offline
        l3u_
        wrote on 8 May 2019, 19:20 last edited by
        #3

        So … should I use const QString text = or const QString &text = ? Are there any (dis)advantages of one version?

        B 1 Reply Last reply 6 Nov 2020, 19:13
        0
        • L l3u_
          8 May 2019, 19:13

          Hi :-)

          Maybe this is a dumb question (maybe caused by lack of C++ knowledge), but I'm not sure if I have faulty reasoning about this:

          When calling a function using e. g. a QString, it seems to be reasonable to me to use (if possible) a const QString & to call it so that no copy of the QString is passed to the function, but only a reference to the actual data. But what about a function returning a QString?

          I have e. g. the following code:

          const QString text = jsonObject.value(someKey).toString();
          someFunction(text);
          someOtherFunction(text);
          

          The following works as well:

          const QString &text = jsonObject.value(someKey).toString();
          someFunction(text);
          someOtherFunction(text);
          

          QJsonValue::toString() returns a QString. So what is referenced in the second case? The return value of the function as long as it doesn't go out of scope? And is it better to do it in this way?

          Thanks for all clarification!

          J Offline
          J Offline
          JonB
          wrote on 8 May 2019, 19:20 last edited by
          #4

          @l3u_
          QStrings already do not work by copying as you assumed, e.g. from docs

          Behind the scenes, QString uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data.

          1 Reply Last reply
          3
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 8 May 2019, 19:52 last edited by
            #5

            Hi
            so basically you can use const QString text and get sort of const QString &text effect.
            This is as mentioned handled by the QString class and makes copying QStrings cheap as they
            share the string data unless one of them modifies it. TL:DR version.. ;)
            const is always good to use for +1 for that.

            1 Reply Last reply
            5
            • L Offline
              L Offline
              l3u_
              wrote on 8 May 2019, 19:53 last edited by
              #6

              Thanks for the clarification :-)

              1 Reply Last reply
              0
              • L l3u_
                8 May 2019, 19:20

                So … should I use const QString text = or const QString &text = ? Are there any (dis)advantages of one version?

                B Offline
                B Offline
                bam80
                wrote on 6 Nov 2020, 19:13 last edited by
                #7

                So … should I use const QString text = or const QString &text = ?

                @l3u_ In my understanding, there is still difference:
                while with implicit sharing copying is cheap, it still implies reference counting, at least until RVO(Return Value Optimization) is not taken in account.
                With const references, we can avoid that unconditionally.

                I'm not sure if there are some conventions about it, though.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 6 Nov 2020, 20:17 last edited by
                  #8

                  @bam80 said in Using a const QString & if a function returns a QString:

                  With const references, we can avoid that unconditionally.

                  Correct, but in the current case of jsonObject.value(someKey).toString(); it does not matter since it returns a QString, not a const QString reference.

                  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
                  2
                  • A aha_1980
                    8 May 2019, 19:18

                    Hi @l3u_

                    what you found is Reference Lifetime Extension - so you referenced a QString.

                    However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often (and that is used within Qt itself also).

                    Regards

                    B Offline
                    B Offline
                    bam80
                    wrote on 7 Nov 2020, 15:10 last edited by
                    #9

                    @aha_1980 said in Using a const QString & if a function returns a QString:

                    However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often

                    BTW, I'm not sure why we are speaking about implicit sharing here - there is no copying occurs anywhere in the original code I suppose (assuming the functions take it's arguments by reference), isn't it?

                    Christian EhrlicherC 1 Reply Last reply 7 Nov 2020, 15:18
                    0
                    • B bam80
                      7 Nov 2020, 15:10

                      @aha_1980 said in Using a const QString & if a function returns a QString:

                      However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often

                      BTW, I'm not sure why we are speaking about implicit sharing here - there is no copying occurs anywhere in the original code I suppose (assuming the functions take it's arguments by reference), isn't it?

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 7 Nov 2020, 15:18 last edited by
                      #10

                      @bam80 said in Using a const QString & if a function returns a QString:

                      there is no copying occurs anywhere in the original code

                      it is:

                      const QString text = jsonObject.value(someKey).toString();

                      At least with c++11 or c++14. See https://en.cppreference.com/w/cpp/language/copy_elision

                      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

                      • Login

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