Using a const QString & if a function returns a QString
-
wrote on 8 May 2019, 19:13 last edited by
Hi :-)
Maybe this is a dumb question (maybe caused by lack of C++ knowledge), but I'm not sure if I have faulty reasoning about this:
When calling a function using e. g. a
QString
, it seems to be reasonable to me to use (if possible) aconst QString &
to call it so that no copy of theQString
is passed to the function, but only a reference to the actual data. But what about a function returning aQString
?I have e. g. the following code:
const QString text = jsonObject.value(someKey).toString(); someFunction(text); someOtherFunction(text);
The following works as well:
const QString &text = jsonObject.value(someKey).toString(); someFunction(text); someOtherFunction(text);
QJsonValue::toString()
returns aQString
. So what is referenced in the second case? The return value of the function as long as it doesn't go out of scope? And is it better to do it in this way?Thanks for all clarification!
-
Hi :-)
Maybe this is a dumb question (maybe caused by lack of C++ knowledge), but I'm not sure if I have faulty reasoning about this:
When calling a function using e. g. a
QString
, it seems to be reasonable to me to use (if possible) aconst QString &
to call it so that no copy of theQString
is passed to the function, but only a reference to the actual data. But what about a function returning aQString
?I have e. g. the following code:
const QString text = jsonObject.value(someKey).toString(); someFunction(text); someOtherFunction(text);
The following works as well:
const QString &text = jsonObject.value(someKey).toString(); someFunction(text); someOtherFunction(text);
QJsonValue::toString()
returns aQString
. So what is referenced in the second case? The return value of the function as long as it doesn't go out of scope? And is it better to do it in this way?Thanks for all clarification!
Hi @l3u_
what you found is Reference Lifetime Extension - so you referenced a QString.
However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often (and that is used within Qt itself also).
Regards
-
wrote on 8 May 2019, 19:20 last edited by
So … should I use
const QString text =
orconst QString &text =
? Are there any (dis)advantages of one version? -
Hi :-)
Maybe this is a dumb question (maybe caused by lack of C++ knowledge), but I'm not sure if I have faulty reasoning about this:
When calling a function using e. g. a
QString
, it seems to be reasonable to me to use (if possible) aconst QString &
to call it so that no copy of theQString
is passed to the function, but only a reference to the actual data. But what about a function returning aQString
?I have e. g. the following code:
const QString text = jsonObject.value(someKey).toString(); someFunction(text); someOtherFunction(text);
The following works as well:
const QString &text = jsonObject.value(someKey).toString(); someFunction(text); someOtherFunction(text);
QJsonValue::toString()
returns aQString
. So what is referenced in the second case? The return value of the function as long as it doesn't go out of scope? And is it better to do it in this way?Thanks for all clarification!
-
Hi
so basically you can use const QString text and get sort of const QString &text effect.
This is as mentioned handled by the QString class and makes copying QStrings cheap as they
share the string data unless one of them modifies it. TL:DR version.. ;)
const is always good to use for +1 for that. -
wrote on 8 May 2019, 19:53 last edited by
Thanks for the clarification :-)
-
So … should I use
const QString text =
orconst QString &text =
? Are there any (dis)advantages of one version?wrote on 6 Nov 2020, 19:13 last edited bySo … should I use
const QString text
= orconst QString &text
= ?@l3u_ In my understanding, there is still difference:
while with implicit sharing copying is cheap, it still implies reference counting, at least until RVO(Return Value Optimization) is not taken in account.
With const references, we can avoid that unconditionally.I'm not sure if there are some conventions about it, though.
-
@bam80 said in Using a const QString & if a function returns a QString:
With const references, we can avoid that unconditionally.
Correct, but in the current case of
jsonObject.value(someKey).toString();
it does not matter since it returns a QString, not a const QString reference. -
Hi @l3u_
what you found is Reference Lifetime Extension - so you referenced a QString.
However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often (and that is used within Qt itself also).
Regards
wrote on 7 Nov 2020, 15:10 last edited by@aha_1980 said in Using a const QString & if a function returns a QString:
However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often
BTW, I'm not sure why we are speaking about implicit sharing here - there is no copying occurs anywhere in the original code I suppose (assuming the functions take it's arguments by reference), isn't it?
-
@aha_1980 said in Using a const QString & if a function returns a QString:
However, as the Qt data classes implement implicit sharing, the first variant is the one you will see more often
BTW, I'm not sure why we are speaking about implicit sharing here - there is no copying occurs anywhere in the original code I suppose (assuming the functions take it's arguments by reference), isn't it?
@bam80 said in Using a const QString & if a function returns a QString:
there is no copying occurs anywhere in the original code
it is:
const QString text = jsonObject.value(someKey).toString();
At least with c++11 or c++14. See https://en.cppreference.com/w/cpp/language/copy_elision