Odd format in Qurl c'tor
-
Hi,
AFAIK, it is a string literal notation. See the Qt StringLiterals namespace.
Basically, you create a QString out of a UTF-16 string. See here for the C++ doc about them.
-
https://doc.qt.io/qt-6.2/newclasses62.html#new-functions-in-namespaces
operator""_qs(const char16_t *str, size_t size)
https://doc.qt.io/qt-6.2/qstring.html#operator-22-22_qs -
Thanks, guys. So, the "u" preceding the string creates a string literal (16-bit flavor), and the "_qs" after the string converts it to a QString. (According to the docs that JoeCFD referenced, _qs needs a 16-bit string literal.)
So...if _qs is a function, why doesn't it look like one in the line I posted above? Is this more new C++ stuff? I'm starting to feel antediluvian...
-
@mzimmers said in Odd format in Qurl c'tor:
So...if _qs is a function,
It isn't, it's an operator :)
QString operator""_qs(const char16_t *str, size_t size)
. So, doubtless by some piece of C++2000 magic,""_qs
does its (compile-time) magic on the string in between"..."
quotes and followed by_qs
. Gotta love C++.BTW,
u"qrc:/xxx/main.qml"_qs
equates toQStringLiteral("qrc:/xxx/main.qml")
which is a bit more understandable.
-
u prefix signifies a char16_t string literal.
_qs suffix is a user defined literal that produces a QString using that literal as the data directly, without runtime allocation.It's nothing new. Both of these are C++11, so way over decade old by now.
-
@JonB said in Odd format in Qurl c'tor:
It isn't, it's an operator :)
OK, so, to further my education:
operator""_qs(const char16_t *str, size_t size)
How does one go about providing str and size to this operator - is it somehow implicit from the line of code I posted originally?
-
@Chris-Kawa said in Odd format in Qurl c'tor:
It's nothing new. Both of these are C++11, so way over decade old by now.
Chris - anything newer than the Reagan administration is new to me. Nice haircut, BTW...
-
How does one go about providing str and size to this operator - is it somehow implicit from the line of code I posted originally?
u"qrc:/xxx/main.qml"
becomes thestr
argument andsize
is calculated by the compiler.The size argument is optional too, so if you'd like to create your own suffix, for example one that converts a string literal to an int at compile time you would do something like:
int operator"" _mysuffix(const char* str) { return atoi(str); }
and then you can use it like this:
int foo = "42"_mysuffix;
Obviously this is not a very useful example, but the string can come from anywhere, so you could do something like take a preprocessor macro with a version string and convert it to an int at compile time. Or if you have a complex number class you could define
MyComplex operator"" _cx(const char*, size_t)
and have it parse at compile time to create a variable of class MyComplex like this:"4+3i"_cx;
.It's worth pointing out that apart from string literals this operator can also take numbers. For example the std::chrono namespace defines a bunch of them for time units, so you can write something like
sleep(250ms)
and no longer wonder what unit the parameter is.