steeps
steeps

Reputation: 65

Detecting if expression is lvalue or rvalue in C

Is there any way of determining whether a given expression is an lvalue or an rvalue in C? For instance, does there exist a function or macro is_lvalue with the following sort of behaviour:

int four() {
    return 4;
}

int a = 4;

/* Returns true */
is_lvalue(a);

/* Returns false */
is_lvalue(four());

I know equivalent functionality exists in C++, but is there any way of doing this in any standard of C? I'm not particularly interested in GCC-specific extensions.

Thank you for your time.

Upvotes: 1

Views: 229

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 222427

The C standard does not provide any method for detecting whether an expression is an lvalue or not, either by causing some operation to have different values depending on whether an operand is an lvalue or not or by generating a translation-time diagnostic message or error depending on whether an operand is an lvalue or not.

C implementations may of course define an extension that provides this feature.

About the closest one can get in strictly conforming C is to attempt to take the address of the expression with the address-of operator &. This will produce a diagnostic message (and, in typical C implementations, an error) if its operand is not an lvalue. However, it will also produce a message for lvalues that are bit-fields or that were declared with register. If these are excluded from the cases of interest, then it may serve to distinguish between lvalues and non-lvalues during program translation.

Upvotes: 1

Related Questions