[Development] User-defined literals for QString (and QByteArray)

Lars Knoll lars.knoll at qt.io
Fri Mar 5 13:40:24 CET 2021


On 5 Mar 2021, at 12:08, Tor Arne Vestbø <Tor.arne.Vestbo at qt.io<mailto:Tor.arne.Vestbo at qt.io>> wrote:


On 3 Mar 2021, at 16:53, Andrei Golubev <andrei.golubev at qt.io<mailto:andrei.golubev at qt.io>> wrote:

QString hello = u"Hello"; // oops, compilation error

This seems like a bug though? From an API point of view, I’d expect this simple assignment to JustWorkTM, without requiring syntactic sugar all over the place. Shouldn’t we fix this, so we don’t need (or leave optional) an explicit _qs suffix?

The explicit _qs suffix would basically replace QStringLiteral. We can’t have a generic QString(char16_t *) constructor that doesn’t copy the data.

I wish we had a good way of adding that overloaded constructor to QString. Unfortunately, we have lots of API that is overloaded on both QString and QStringView. Adding the constructor to QString will then cause ambiguities wherever we have overloaded API taking both a QString and a QStringView.

I wish there was a way to tell the compiler to prefer one conversion over the other ("if you have the choice, always use QStringView and not QString”), but C++ doesn’t have a way to do that.

Cheers,
Lars

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20210305/c6217d16/attachment.html>


More information about the Development mailing list