Usage of string::c_str on temporary string [duplicate]
This question already has an answer here: C++ destruction of temporary object in an expression 4 answers In regards to when temporary objects get destroyed, is this valid: FILE *f = fopen (std::string ("my_path").c_str (), "r"); Will the temporary be destroyed immediately after having evaluated the first argument to fopen or after the fopen call. Testing with the following code: #include <cstdio> using namespace std; struct A { ~A() { printf ("~A\n"); } const char *c_str () { return "c_str"; } }; void foo (const char *s) { printf ("%s\n", s); } int main () { foo (A().c_str()); printf ("after\n