Is the C++ code below well-formed? Will the std::string
get destroyed before or after the function finishes executing?
void my_function(const char*);
...
my_function(std::string("Something").c_str());
I know I could do my_function("Something")
, but I am using std::string
this way to illustrate my point.
string
will die at the end of the expression. That'll be after the function call. So long asmy_function
doesn't store that pointer somewhere with a longer life you're good. – Drucillmy_function("Something");
. I mean, sure,std::string
is widely favored over C strings in C++, but when you have a function that wants aconst
C string anyway, how does it make sense to use astd::string
to make a temporary copy of a perfectly good C string to pass to it? – Coil