need help on a syntax in conditional operator

B

Bill Davidson

Hello there,

I need help on understanding part of the macro below:

#define MY_MACRO( OP, RESULT ) \
( (RESULT = (OP)) != 0 ? (errno = RESULT, -1) : 0 )

What I do not understand is how is the part (errno = RESULT, -1)
get evaluated.

Bill
 
R

Rob Williscroft

Bill Davidson wrote in
in comp.lang.c++:
Hello there,

I need help on understanding part of the macro below:

#define MY_MACRO( OP, RESULT ) \
( (RESULT = (OP)) != 0 ? (errno = RESULT, -1) : 0 )

What I do not understand is how is the part (errno = RESULT, -1)
get evaluated.

The default behaviour of the , (comma) operator is to evaluate
the left expression and dicard the result and then to evaluate
the right expression:

value = ( evalute_and_dicard, result );

value = ( evalute_and_dicard, evalute_and_dicard_2, result );

Both of the above would assign result to value.

Assuming OP and RESULT in your example above are int's we could
rewrite it as:

inline int my_function( int op, int &result )
{
int return_value;

result = op;
if ( result != 0 )
{
errno = result;
return_value = -1;
}
else
{
return_value = 0;
}

return return_value;
}

HTH

Rob.
 
C

Claudio Jolowicz

#define MY_MACRO( OP, RESULT ) \
( (RESULT = (OP)) != 0 ? (errno = RESULT, -1) : 0 )

What I do not understand is how is the part (errno = RESULT, -1)
get evaluated.

The comma in between the assignment and -1 is the sequencing operator.
The expressions are evaluated from left to right: RESULT is assigned to
errno, and the overall value will be -1.

BTW you are more likely to find this sort of code in C than in C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top