c++ - Confusing L-Value and R-Values parentheses -


in example here @ bottom, there exemplary l-values defined:

  // lvalues:   int& foo();   foo() = 42; // ok, foo() lvalue   int* p1 = &foo(); // ok, foo() lvalue 

i not shure foo() here? @ first sight looks function/method?

is int& foo() same int& foo; ?

but on other hand, compiler says

error: 'foo' declared reference not initialized int & foo;

same rvalues foobar():

  // rvalues:   int foobar();   int j = 0;   j = foobar(); // ok, foobar() rvalue   int* p2 = &foobar(); // error, cannot take address of rvalue 

yes, "a function/method", returning reference int. or, more precisely, declaration of such function, not definition: says such function exists, not provide actual code (because code not relevant example). compare how define functions in header files.

a possible example of code similar function:

int a, b; int& foo(bool which) {     if (which) return a;     else return b; }   ... foo(true) = 10; // works = 10; 

Comments