+ 1
What is/could be the purpose of this kind of C++ statement?
Statement: const std::string &str_var = "some string"; What does a reference to a constant of a data type (i.e. not a variable) achieve? Aren't references already const by default? Even without the const, what would be the purpose of initializing a reference to a literal value? Isn't a reference to a literal string (for an example) just a string variable? Thanks ahead of time!
11 ответов
+ 2
const & means just: Don't copy this constant. It's not required to be an lvalue because it's a constant anyways.
+ 2
You want to avoid copies for performance purposes. Note that with integrated types, it's not worth it.
+ 2
Joseph Carpinelli It's really just a performance concern.
+ 2
Thanks, everyone.
+ 2
So you're satisfied?
+ 1
So it would be to prevent creating a copy of the string when being used, instead using the actual string itself (which is now an rvalue with a name?)? What would be the usefulness of this? I have more questions, but think I may be missing something fundamental so will hope for a clarifying response before putting more effort into inquiring about a false concept/understanding.
An example would be very helpful, if possible.
Thank you very much!
+ 1
Example:
void someMethod(const std::string &str)
{
std::cout<<str;
}
Since we do not make any operations on it, we make it constant reference, which means "Give me the original object and make it constant, so no one may change it".
If you want some harder examples, you can check out my code:
https://code.sololearn.com/cDSUBZaQgU6p/?ref=app
+ 1
I appreciate the answer, Jakub Stasiak, but I'm referring to when a reference type is INITIALIZED with an RVALUE, not references to lvalues.
I generally get what Timon is trying to say, but both fail to answer the entire question. I'm still feeling dissatisfied with my understanding of this, despite searching and asking here.
I'm looking forward to anyone else who can chime in or add any extra details (or explain to me why what I'm asking doesn't make sense or is coming from a place of fundamental misunderstanding). The questions are fairly explicit, but let me know if I could add more info to help answer this.
Thanks, everyone!
+ 1
For the most part. I appreciate your help.
So is it accurate to say we are initializing a reference to an rvalue (e.g. an explicit string not assigned to memory anywhere) in order to to avoid performance issues when using that string? Is that even an accurate description of what is happening? Are there performance lags with copying strings? Does the const have to be in there? What if we are only iterating through the string to use characters for assignment, and not putting it into a function (like a base64 table string)? Are strings one of the most basic/simple data type to use this with or would there be a significant performance boost for other types part of standard C++? Whats the difference between having a const reference to a string, and just using that literal string? Is it just to basically give it a name?
I know it's a lot, so I understand if I am asking too much to answer on a touch keyboard.
Thanks again!
+ 1
const std::string& foo = "Hello.";
has no performance boost. But
const std::string& view = str;
has.
void print(const std::string& msg);
print(str);
has a performance gain.
print("Hello");
Why not?
Rule of thumb: It's worth it for large arrays and types which are no POD.
0
Jakub Stasiak, for your example, do you have a constant reference just for performance reasons? Are there reasons to pass something by reference and not change it (not including the mentioned performance reasons)? Thanks!