I've seen other posts regarding this but I'm still quite a bit confused regarding this.
const std::string& func(std::string&& ref) { return ref; }
int main(void) {
const std::string& r=func("E");
std::cout << r << std::endl;
}
Now this should technically be UB, I'm returning a reference to "E" which should be destroyed after the expression that assigns to r is done, right? However, no warning from clang or g++, perhaps I am missing something?
const std::string& func(std::string ref) { return ref; }
int main(void) {
const std::string& r=func("E");
std::cout << r << std::endl;
}
Clang would warn about something like this. But, it doesn't warn about this:
const std::string& func(std::string ref) { return std::move(ref); }
int main(void) {
const std::string& r=func("E");
std::cout << r << std::endl;
}
It also doesn't warn about something like:
std::string&& func(std::string&& ref) { return std::move(ref); }
int main(void) {
std::string&& r=func("E");
std::cout << r << std::endl;
}
Is this not UB after all?
If someone could clarify on these I'd be grateful.