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.
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::stringobject here is such a temporary materialized in the full-expression that spans the wholemy_function(std::string("Something").c_str());expression statement. So it is destroyed aftermy_functionreturns in your example.