Results 1 to 17 of 17

Thread: QString::append() weirdness

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23
    Thanked 15 Times in 11 Posts

    Default Re: QString::append() weirdness

    Thank you for your efforts Wysota.

    It turns out the code does not work "because the standard says so". In other words, while this code is ok:

    Qt Code:
    1. // bind a temporary *expression* to a const reference
    2. const QString& s = QString("hello");
    To copy to clipboard, switch view to plain text mode 

    this one leaves a dangling reference:

    Qt Code:
    1. // bind a temporary *object* to a const reference
    2. const QString& s = QString("hello").append("");
    To copy to clipboard, switch view to plain text mode 

    I believe that from a purely "technical" point of view there is nothing wrong with the second snippet (a compiler could easily generate working code), but in fact it is not supposed to work.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QString::append() weirdness

    Thankfully with the "imfamous implicit sharing" Qt offers you can happily do this:
    Qt Code:
    1. QString s = QString("hello").append("");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23
    Thanked 15 Times in 11 Posts

    Default Re: QString::append() weirdness

    LOL

    You know, I am a bit nervous about "implicit sharing" since I reported this bug (read: feature):
    http://bugreports.qt.nokia.com/browse/QTBUG-12941

    In short, though QLinkedList offers the same "iteration" interface of a std::list (begin(), end()), the behaviour of the iterators it provides is not standard conforming.

    For example, the standard says that a std::list::end() iterator should remain constant through the lifetime of a list. But calling QLinkedList::clear() can in fact change the value returned by QLinkedList::end().

    So, when in my code I blindly replaced some std::lists with QLinkedLists I got crashes everywhere.

    Nothing to pull your hair about, of course, but still annoying... and guess who's to blame for that? :-)

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QString::append() weirdness

    I fail to see how this can make you nervous about implicit sharing itself. As long as you don't call clear() on the list the behaviour will be as you expect it. And the behaviour of clear() is not related to implicit sharing itself but rather to limiting memory usage of empty lists (which happens to be implemented using implicit sharing).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: QString::append() weirdness

    Besides that, changing the items while iterating over them is not safe.

Similar Threads

  1. [SOLVED] quint16 weirdness
    By pdoria in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2009, 01:09
  2. Cannot append to QFile using QIODevice::Append
    By philwinder in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2008, 10:09
  3. Need help. can't append chars in QString
    By AcerExtensa in forum Qt Programming
    Replies: 6
    Last Post: 12th June 2008, 11:57
  4. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 19:51
  5. QSplashScreen Weirdness
    By mclark in forum Qt Programming
    Replies: 11
    Last Post: 19th November 2007, 07:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.