Thursday, 15 March 2018

c++ - Function returning int&

I was looking around the net and found an article which tries to explain std::move and rvalues and found something I really can't grasp.





int i = 42; 
i = 43; // ok, i is an lvalue
int* p = &i; // ok, i is an lvalue
int& foo();
foo() = 42; // ok, foo() is an lvalue
int* p1 = &foo(); // ok, foo() is an lvalue




What's the point of int& foo()? What is foo() = 42 in this example?



EDIT: Since the part that got me confused and doesn't have anything to do with rvalues I'll write the part that solved my problem:



I didn't know you could write function declarations inside scopes so int& foo() became a int reference with empty initializer which also is impossible since references must point to something. But in the heat of the moment I went full .......

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...