Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

+i returns lvalue?

Tags:

c

Below code gives error on compilation in a C compiler

++(-i);
error: lvalue required as increment operand

It means that -i returns rvalue.

while code

++(+i);

don't give any error . Why so? this link says that +i don't result in lvalue.

like image 396
pawan Avatar asked Aug 18 '12 15:08

pawan


People also ask

Can a function return an lvalue?

Why is it not possible to set a function returning an address while it is possible to set a function that returns a reference. Short answer: You return a pointer by-value and anything returned by-value is (by definition) not an lvalue.

How do you return variables?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

What is return type in Cpp?

The result of a function is called its return value and the data type of the return value is called the return type. Every function declaration and definition must specify a return type, whether or not it actually returns a value.

What is return value in C++ function?

In C++, a function which is defined as having a return type of void , or is a constructor or destructor, must not return a value. If a function is defined as having a return type other than void , it should return a value.


1 Answers

It is a glitch in your compiler. In C language all lvalues in expressions are converted to rvalues even before any operators are applied, with the exception of operands of sizeof, &, ++, -- and left-hand sides of . and assignment (see 6.3.2/2)

In other words, in C language +i must produce an rvalue not because unary + supposedly produces an rvalue, but rather because i is converted to rvalue before that unary + even gets a chance to do its thing.

For example, for an int i variable that holds value 42, expression +i is fully equivalent to expression +42. The lvalueness of i is lost and it gets turned into 42 before the semantics of unary + comes into play.

Needless to say, in this case there's no chance the result of unary + can be an lvalue.

like image 136
AnT Avatar answered Sep 27 '22 23:09

AnT