I have a constant string value
std::string name_to_use = "";
I need to use this value in just one place, calling the below function on it
std::wstring foo (std::string &x) {...};
// ...
std::wstring result = foo (name_to_use);
I can simply not declare the variable and use a string literal in the function call instead, but to allow easy configuration of name_to_use
I decided to declare at the beginning of the file.
Now, since I am not really modifying name_to_use
I thought why not use a #define
preprocessing directive so I do not have to store name_to_use as a const anywhere in memory while the main program runs continuously (a GUI is displayed).
It worked fine, but then I came across constexpr
. A user on stackoverflow has said to use it instead of #define
as it is a safer option.
However, constexpr std::string name_to_use
is still going to leak memory in this case right? Since it's not actually replacing occurrences of name_to_use
with a value but holding a reference to it computed at compile time (which does not offer me any benefit here anyway, if I'm not mistaken?).