I was reading up on rvalues and ran into something while trying a code snippet on compiler explorer. Here's a contrived example:
class A{ public: A&& func(A& rhs) { //return rhs; return std::move(rhs); //returning A&& }};
vs
class A{ public: A func(A& rhs) { //return rhs; return std::move(rhs); //returning A&& }};
I was expecting this second snippet to fail to compile because of return type mismatch. Func's return type here is A
whereas what it's actually returning is A&&
courtesy of std::move()
. I have tried latest versions of gcc, clang and msvc. All have this same behaviour. Can someone please explain what's going on here?