diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2011-10-20 12:47:04 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-04-23 23:26:28 +0200 |
commit | d631c31235dc3ab682ba5d4cbb466680d47b36f2 (patch) | |
tree | f8c3ed874572caa33fa632451fa31b0b4ea354b1 /src/corelib/tools/qstring.h | |
parent | 33371c016e13dadcf7535e11e38065ab193b2c6e (diff) |
Make QStringLiteral and QByteArrayLiteral always return the real types
Up until now, the macros would return an internal type that contained
the pointer to the data. This breaks code that tried to use the macros
with operators, like QStringBuilder but also when writing:
QStringList() << QStringLiteral("a") << QStringLiteral("b");
This change seems to work fine now and I can also verify that this
works:
const auto str = QStringLiteral("Hello");
Even though it creates a QString, which is non-POD and non-constexpr.
Change-Id: Iaf82af9bea4245513a1128ea54f9d2d3d785fb09
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com>
Diffstat (limited to 'src/corelib/tools/qstring.h')
-rw-r--r-- | src/corelib/tools/qstring.h | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index edb140b682..a96046d837 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -113,13 +113,14 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, # if defined(Q_COMPILER_LAMBDA) # define QStringLiteral(str) \ - ([]() -> QStringDataPtr { \ + ([]() -> QString { \ enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \ static const QStaticStringData<Size> qstring_literal = { \ Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \ QT_UNICODE_LITERAL(str) }; \ QStringDataPtr holder = { qstring_literal.data_ptr() }; \ - return holder; \ + const QString s(holder); \ + return s; \ }()) \ /**/ @@ -129,14 +130,14 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, // To do that, we need the __extension__ {( )} trick which only GCC supports # define QStringLiteral(str) \ - __extension__ ({ \ + QString(__extension__ ({ \ enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \ static const QStaticStringData<Size> qstring_literal = { \ Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \ QT_UNICODE_LITERAL(str) }; \ QStringDataPtr holder = { qstring_literal.data_ptr() }; \ holder; \ - }) \ + })) \ /**/ # endif @@ -144,9 +145,10 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, #ifndef QStringLiteral // no lambdas, not GCC, or GCC in C++98 mode with 4-byte wchar_t -// fallback, uses QLatin1String as next best options +// fallback, return a temporary QString +// source code is assumed to be encoded in UTF-8 -# define QStringLiteral(str) QLatin1String(str) +# define QStringLiteral(str) QString::fromUtf8(str, sizeof(str) - 1) #endif #define Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \ |