Return Statement in Ternary Operator C++

Ternary operators and Return in C

return is a statement. Statements cannot be used inside expressions in that manner.

C – How to use an exit/return statement in ternary operator?

How to use an exit/return statement in ternary operator?

You cannot. The second and third operands of a ternary operator must be expressions, and a return statement is not an expression (see Ternary operators and Return in C). It is, however, allowed for the two to both be void expressions, such as exit(1), in which case the result of the operation is also a void expression.

Additionally, with respect to your specific code, the comma operator (,) has the lowest precedence of all C operators. Therefore, if you intend to use a comma expression as an operand in any other operation, then you need to parenthesize it.

I wanted to formulate these two if statements in a shorter way:

Why? Mere conciseness or brevity is not a useful objective. If it makes your code hard for a human to read and understand, then making your code shorter is a liability.

But if you mean that you want to express the same thing more simply, more clearly, and / or less redundantly, then that can lead you toward forms that also happen to be shorter. For example, you can encapsulate the logic in a function or macro. Personally I often supply a macro that looks something like this:

#define ERROR_IF_NZ(cond, message) do { \
if (cond) { \
fputs(message, stderr); \
exit(1); \
} \
} while (0)

Using that, your code could look like this:

ERROR_IF_NZ(argc != 2, "Error X\n");
ERROR_IF_NZ(NULL == file, "Cannot open file!\n");

Adjust the macro name so that it makes the most sense to you, and look! It is not only clearer than the original code, but shorter, too, even if you count the length of the macro definition.

Update

Edits to the question have pretty much mooted this answer. If you already have functions that serve the the same role that a macro such as described here would do, then just call them. There is no redeeming value in replacing the functions and the calls to them with a ternary expression, nor is it worth any effort to convert the functions you have into macros. Certainly, reducing source code size is not an reasonable motivation for such changes.

return statement in ternary operator c++

The second and third arguments to the ternary operator are expressions, not statements.

 return a

is a statement

Ternary operator inside a return statement

The ternary conditional operator has lower precedence than operator+.
Your code was actually parsed as

(SumOfEvenNumbers(v, i+1) + (v[i]%2==0)) ? v[i] : 0;

To get what you want, use parentheses

SumOfEvenNumbers(v, i+1) + ((v[i]%2==0)) ? v[i] : 0);

See: http://en.cppreference.com/w/c/language/operator_precedence

return statement with conditions ? and :

It means

if (ch == X) 
return O;
else
return X;

Ternary operator gives unexpected results in return statement

The ternary operator finds a common type and value category of both operands, and this doesn't happen for the two other cases, which is why it works there.

months_.Last() and Month() both have type Month, so everything is fine there. But now, let's examine the value categories. months_.Last() is an lvalue, while Month() is a prvalue! So, the common value category here is a prvalue, and both operands get converted to a prvalue. Which means that you get a new Month from months_.Last() every time (from the copy)!

Note again however, that this is a MS specific extension. Without that extension, the code would be invalid, as you would try to bind the prvalue returned by the conditional operator to a non-const lvalue reference.

Why can't we have return in the ternary operator?

The ternary operator evaluates to an expression and expressions can't contain a return statement (how would that behave if you were to assign the expression to a variable?). However, you could very well return the result of a ternary operator, i.e. return condition? returnValue1 : returnValue2;

On your specific point, I don't see why you would like to return. It looks like you're trying to do something only if a condition is fulfilled. A simple if statement would probably be more adequate there.



Related Topics



Leave a reply



Submit