samedi 18 juillet 2015

Function returning int& [duplicate]

This question already has an answer here:

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 .......

Aucun commentaire:

Enregistrer un commentaire