[Development] RFC: Proposal for a semi-radical change in Qt APIs taking strings

Knoll Lars Lars.Knoll at theqtcompany.com
Wed Oct 21 08:29:30 CEST 2015


On 20/10/15 18:11, "development-bounces+lars.knoll=theqtcompany.com at qt-project.org on behalf of Thiago Macieira" <development-bounces+lars.knoll=theqtcompany.com at qt-project.org on behalf of thiago.macieira at intel.com> wrote:



>On Tuesday 20 October 2015 08:49:17 André Somers wrote:
>> Op 19-10-2015 om 17:54 schreef Thiago Macieira:
>> > First of all, QStringRef keeps a pointer to the original QString, so it
>> > isn't as fragile as QStringView. In fact, during the development of the
>> > XML stream classes, there was a QSubString class that did more or less
>> > what QStringView would do. It got replaced for a reason.
>> 
>> Would you mind expanding on that a bit? I would think that the XML
>> stream classes would be a natural example to use a string view, but it
>> seems that such a thing was tried and dragons were uncovered... Perhaps
>> it would be good to share a war story here so we won't run into those
>> same dragons again?
>
>Wish that I could. I wasn't around that time. I know this happened because I 
>used to pull the daily snapshots and commit to an svk repository (yes, svk!) 
>to see the differences. That's how I spotted the QSubString class being added, 
>then removed, then QStringRef showed up.
>
>Is there anyone still around who worked with Matthias on this back in 
>2006/2007?

I was there doing the work with Matthias. 

As I remember it, the change from QSubString to QStringRef was a simple API naming decision, i.e. independent of the implementation details. 

The reason a String pointer is in there was due to an attempt to make this class a bit safer. As long as we guaranteed the lifetime of the underlying String, accessing the string ref should be ok. In hindsight, I do think this was a mistake, as this safety doesn't give you a whole lot. In practice, you couldn't modify the underlying Qstring anyway, so we could have simply used a QChar pointer and a length. That certainly would have been more flexible and would have helped us today.

So I'd agree that one could use a QStringView in the XML parser today. QStringRef is doing exactly the same, but has the disadvantage of requiring a pointer to a QString. This works in the parser, as it keeps the whole string in memory for as long as parsing is ongoing, and you are supposed to consume the returned stringref. But I would not recommend using QStringRef/QStringView as a return value in almost all other cases.

Cheers,
Lars



More information about the Development mailing list