[Development] QStringLiteral is broken(ish) on MSVC (compiler bug?)

Matthew Woehlke mwoehlke.floss at gmail.com
Thu Mar 14 21:50:09 CET 2019

While working on some modernization of my application — in particular,
converting some UTF-8 literals to use QStringLiteral — I noticed a
concerning compiler warning:

  warning C4566: character represented by universal-character-name
  '\uXXXX' cannot be represented in the current code page (1252)

After doing some testing, it turns out that, given code like
QStringLiteral("\u269E \U0001f387 \u269F"), MSVC is indeed butchering
the string.

Further investigation shows that the problem seems to be with the
implementation of QStringLiteral. In particular, it appears that the
preprocessor initially sees just the raw string literal without the 'u'
prefix, butchers it, then later "promotes" it to a UTF-16 literal, but
by then the damage has been done.

While this absolutely feels like a compiler bug, it's an *awful* big
gotcha that probably should be documented. Also, is there anything that
Qt can do to work around it? (I know these sorts of macro expansions can
be tricksy...)

Note: and the *local* work-around is apparently to include the 'u'
prefix on my own literal; apparently doubling it (`uu"stuff"`) is okay.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: UnicodeTest.cxx
Type: text/x-c++src
Size: 335 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20190314/78e31fb2/attachment.cxx>

More information about the Development mailing list