#include <string>
#include <iostream>
void Print(const char* s) {
std::cout << s << std::endl;
}
std::string GetString() {
std::string ret;
ret = "Is this defined behavior?";
return ret;
}
int main() {
Print(GetString().c_str());
}
This code seems to work reliably on the big 3 compilers. I'm concerned that it might not actually be defined behavior, is it possible the string could be destroyed leaving the c_str() returned pointer dangling before the Print function is called?