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.
Will the std::string get destroyed before or after the function finishes executing?
Temporary objects are destroyed (with some exceptions, none relevant here) at the end of the full-expression in which they were materialized (e.g. typically the end of an expression statement).
The std::string
object here is such a temporary materialized in the full-expression that spans the whole my_function(std::string("Something").c_str());
expression statement. So it is destroyed after my_function
returns in your example.