I know that string literals in C/C++ have static storage duration, meaning that they live "forever", i.e. as long as the program runs.
Thus, if I have a function that is being called very frequently and uses a string literal like so:
void foo(int val)
{
std::stringstream s;
s << val;
lbl->set_label("Value: " + s.str());
}
where the set_label function takes a const std::string&
as a parameter.
Should I be using a const std::string
here instead of the string literal or would it make no difference?
I need to minimise as much runtime memory consumption as possible.
edit:
I meant to compare the string literal with a const std::string prefix("Value: ");
that is initialized in some sort of a constants header file.
Also, the concatenation here returns a temporary (let us call it Value: 42
and a const reference to this temporary is being passed to the function set_text()
, am I correct in this?
Thank you again!