Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-69388

Strange behaviour of std::vector<std::pair<QString,int*> >

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • None
    • 5.11.1
    • None

    Description

      (As reported on StackOverflow.)

      The following code produces an unexpected output when compiled with g++ main.cpp -O3 -std=c++17 -Wall -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core (GCC version 8.1.1, Qt version 5.11.1, Arch Linux).

      #include <QString>
      #include <QDebug>
      #include <vector>
      #include <tuple>
      
      int main() {
          typedef std::pair<QString,int*> A;
          std::vector<A> v;
          v.emplace_back("111", nullptr);
          v.emplace_back("222", nullptr);
          v.emplace_back("333", nullptr);
          v.emplace_back("444", nullptr);
          v.emplace_back("555", nullptr);
          for (const A &a : v)
              qDebug() << a.first;
          return 0;
      }
      

      The output is as follows.

      ""
      ""
      ""
      ""
      "555"
      

      It seems like the vector resize triggers some problem with the move constructor.

      Adding the -fno-strict-aliasing compiler option circumvents the problem. (According to the GCC web page, this is a hint that it's a bug in the code, not in GCC.)

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            villevoutilainen_qt Ville Voutilainen
            fagu fagu
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes