According to cppreference, in return statement, local rvalue references should be moved from in C++20:
Automatic move from local variables and parameters
If expression is a (possibly parenthesized) id-expression that names a variable whose type is either
a non-volatile object type or a non-volatile rvalue reference to object type (since C++20)
I tried the second bullet point (godbolt):
#include <cstdio>
struct Foo {};
struct Bar {
Bar() = default;
Bar(const Foo &) {
printf("copy!\n");
}
Bar(Foo &&) {
printf("move!\n");
}
};
Bar foo() {
Foo f;
Foo &&rf = static_cast<Foo &&>(f);
return rf;
}
int main() {
foo();
}
I expected that this program prints move!. But actually, both clang and gcc prints copy!. Is this something which haven't been implemented yet in compilers? Or do I misunderstand something?