Thursday, March 15, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...