Consider the following functions. I'd like answers for C++17.
MyClass&& func() {
return MyClass{};
}
int main() {
MyClass&& myRef = func();
}
Questions:
- Is the expression
func()an xvalue? Why? - Why is
myRefa dangling reference? Or, more specifically, why isfunc()returning a dangling reference? Wouldn't returning rvalue reference cause temporary materialization, and extend the temporary object's lifetime?