Lets take a look to this two functions:
std::string get_string()
{
std::string ret_value;
// Calculate ret_value ...
return ret_value;
}
void process_c_string(const char* s)
{
std::cout << s << endl;
}
And here are two possible calls of process_c_string
with argument returned by get_string
.
Without binding const reference to the returned object of
get_string
.process_c_string(get_string().c_str());
With binding const reference to the returned object of
get_string
.const std::string& tmp_str = get_string(); process_c_string(tmp_str.c_str());
I know that second way is valid, but what about the first one, what does standard say about this case? Will the temporary object returned by get_string
be deleted before process_c_str
finished because of there is no const reference
to it?
Note: The both versions are ok in MSVC.