I need to change from operator""_qs
to operator""_s
, thanks to Qt 6.8. The following code compiles
// notice no includes or using namespace of any kind
auto const label = u"Foo"_qs;
So I tried the following update
#include <StringLiterals>
using namespace Qt::StringLiterals;
auto const label = u"Foo"_s;
but error C3688: invalid literal suffix '_s'; literal operator or literal operator template 'operator ""_s' not found
My first question is, how does it find operator""_qs
without any include or namespace action?
And the second is obviously, how do I fix the error? I have the suspicion that cmake isn't loading the newest Qt version although I have installed only Qt6.6.
ChatGPT solved it:
In Qt 6.6 and newer, the _qs
string literal (i.e., operator""_qs
) is a built-in part of the QtCore
module. It was introduced specifically to allow more seamless use of QString in a modern C++ way.
Here's why it "just works":
operator""_qs
is a global literal operator, defined in Qt's headers.C++20
modules support or precompiled headers may auto-import QtCore
's built-ins in some build setups.qstringliteral.h
, so it’s very likely included transitively through headers like QString, even if not explicitly shown in your snippet.So, even without includes or using directives, _qs
can work depending on the context of the full translation unit.
No, you cannot use _s
as a QString literal
in Qt 6.6 — it's a feature introduced in Qt 6.8.
In Qt 6.6:
_qs
works — it's part of the built-in support for QStrings added in Qt 6.6._s
does not work — the operator""_s
is part of the Qt::StringLiterals
namespace, added in Qt 6.8.<QtCore/StringLiterals>
exists in Qt 6.6 but does not define operator""_s
yet.So there's no way to make _s
work as a QString literal in Qt 6.6, unless you backport the operator yourself (not recommended in production code).