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 does const auto& lead to empty lists?
Forum Updated to NodeBB v4.3 + New Features

Why does const auto& lead to empty lists?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
9 Posts 4 Posters 2.0k Views 4 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    Given that const references extend the lifetime of object instances (see e.g. Herb Sutter's explanation), why does the following code dump an empty list?

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        const auto& list = QStringList() << "abc"
                                         << "xyz";
        qDebug() << list;
    
        return a.exec();
    }
    

    Change to "const auto list =[...]", and the code works as expected.

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

      Hi,

      What compiler are you using ?

      This works with Apple LLVM version 9.0.0 (clang-900.0.39.2)

      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
      1
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        Both MinGW 5.3 and VS 2015 show this behavior in my tests.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Konstantin Tokarev
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What about VS2017 ?

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

            A 1 Reply Last reply
            0
            • A Asperamanca

              Given that const references extend the lifetime of object instances (see e.g. Herb Sutter's explanation), why does the following code dump an empty list?

              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
              
                  const auto& list = QStringList() << "abc"
                                                   << "xyz";
                  qDebug() << list;
              
                  return a.exec();
              }
              

              Change to "const auto list =[...]", and the code works as expected.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @Asperamanca said in Why does const auto& lead to empty lists?:

              const references extend the lifetime of object instances (see e.g. Herb Sutter's explanation)

              Wow, TIL. I must say this feature feels unintuitive to me, however.

              EDIT: Actually, this felt weird because I've never found a reason to define a const-ref local variable. However, this feature makes some sense when I think about a const-ref function parameter.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              A 1 Reply Last reply
              1
              • JKSHJ JKSH

                @Asperamanca said in Why does const auto& lead to empty lists?:

                const references extend the lifetime of object instances (see e.g. Herb Sutter's explanation)

                Wow, TIL. I must say this feature feels unintuitive to me, however.

                EDIT: Actually, this felt weird because I've never found a reason to define a const-ref local variable. However, this feature makes some sense when I think about a const-ref function parameter.

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @JKSH said in Why does const auto& lead to empty lists?:

                @Asperamanca said in Why does const auto& lead to empty lists?:

                const references extend the lifetime of object instances (see e.g. Herb Sutter's explanation)

                Wow, TIL. I must say this feature feels unintuitive to me, however.

                EDIT: Actually, this felt weird because I've never found a reason to define a const-ref local variable. However, this feature makes some sense when I think about a const-ref function parameter.

                So you never do stuff like this?

                for(const auto& item : container)
                {
                   // Do stuff
                }
                

                Because, theoretically you could now remove that item from the container within that loop (although this would rarely be a good idea), and the reference would not be invalid.

                JKSHJ 1 Reply Last reply
                0
                • SGaistS SGaist

                  What about VS2017 ?

                  A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by
                  #8

                  @SGaist said in Why does const auto& lead to empty lists?:

                  What about VS2017 ?

                  Same as VS2015 and MinGW

                  1 Reply Last reply
                  0
                  • A Asperamanca

                    @JKSH said in Why does const auto& lead to empty lists?:

                    @Asperamanca said in Why does const auto& lead to empty lists?:

                    const references extend the lifetime of object instances (see e.g. Herb Sutter's explanation)

                    Wow, TIL. I must say this feature feels unintuitive to me, however.

                    EDIT: Actually, this felt weird because I've never found a reason to define a const-ref local variable. However, this feature makes some sense when I think about a const-ref function parameter.

                    So you never do stuff like this?

                    for(const auto& item : container)
                    {
                       // Do stuff
                    }
                    

                    Because, theoretically you could now remove that item from the container within that loop (although this would rarely be a good idea), and the reference would not be invalid.

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @Asperamanca said in Why does const auto& lead to empty lists?:

                    So you never do stuff like this?

                    for(const auto& item : container)
                    {
                       // Do stuff
                    }
                    

                    I do, I do.

                    I just I think of this like I do with function parameters, but I think of function parameters differently from local variables inside a function block (like const auto& list = ...;). Once I remember they're basically the same, it now makes sense.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    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