c++ - Returning temporary, why not a rvalue reference? -
a rvalue reference temporary object right? why isn't following compiling? thought function returned rvalue reference
main.cpp:40:12: error: no viable conversion 'hello3 ()' 'hello4' hello4 lol = returning;
in code
#include <iostream> #include <string> #include <vector> class hello { public: }; class hello2 { public: }; class hello3 { public: hello obj1; hello2 obj2; }; class hello4 { public: hello4(hello3&&) { std::cout << "he"; } }; hello3 returning() { hello a; hello2 b; return {a,b}; } int main() { hello4 lol = returning; }
i read move semantics documents still don't understand why above doesn't bind rvalue reference
a rvalue reference temporary object right?
no, it's reference object might or might not temporary.
why isn't following compiling?
because left (empty) argument list off function call:
hello4 lol = returning(); ^^
the error message indicates code tries assign function, not result of calling function, variable.
i thought function returned rvalue reference
no, returns object, since there's nothing reference refer to. temporary object can bind rvalue reference, can used construct hello4
.
Comments
Post a Comment