Question

Out of curiosity I started wondering if it's possible to have a ternary expression that, if it evaluates to false, does nothing in the false branch.

Ie is there a way to write something like this:

variable = (someBool) ? i : <do nothing>;

As opposed to:

if (someBool) {
    variable = i;
}

I tried ((void)0) or while(false){}; as no-op but the compiler expects an expression.

UPDATE:

I realized the question lost some meaning because I tried to make the code easier. The initial idea I had was to initialize a static var with a ternary - using the static var itself as the condition:

static int var = (var != 0) ? var = 1 : (var already initialized, do nothing);

This is assuming that uninitialized variables are initialized to 0 which is not always true (or never in release builds, not quite sure). So maybe it's a hypothetical question.

Was it helpful?

Solution

how about short-circuit?

int variable = 0;
bool cond = true; // or false

(cond && (variable = 42));

printf("%d\n", variable);

OTHER TIPS

How about this:

variable = (someBool) ? i : variable ;

Though I would personally prefer the original if statement

Compilers not only expect expression, but the expression the returns type on the left side (the type of variable whatever is it). So, no you can not do that. It's not conditional execution, but variable member assignment.

These are completely different things. In second example :

if (someBool) {
    variable = i;
}

you do not assign anything, but simply execute based on condition. So in your case, where you don't want to do anything (not assign anything), the way to go is conditional execution so use simply the second case.

The format of the conditional expression is

<expression> ? <expression> : <expression>

In other words, it must have some expression.

Addressing your edit: in C99 variables of static scope are initialised to 0. However, I have never really trusted that because I've been programming in C since the K&R days.

Anyway, just initialise the variable. As the variable is static, it's only going to happen once during the whole execution time of the program.

You could do:

variable = !someBool ?: i;

Since the ?: will no-op when the if expression is true but assign i if it's false.

Note: This has only been tested in Obj-C

How about

(someBool) ? (variable = i) : NULL;

For C# says: Syntax:

condition ? first_expression : second_expression;

And it says about first_expression and second_expression:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

If you were to evaluate a nullable object type instead of bool, you could always write:

variable = myVar ?? i;

Hacky/cludgey/impractical - probably all 3, but for the sake of this question it's a way of omitting an 'else'.

Try: null lambda.

auto null_lambda = [](){return;};
int a = 1;
int b = 2;
vector<int> c;

a > c ? b = c.push_back(b) : null_lambda();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top