Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to annotate a ternary expression with `[[likely]]`?

Tags:

c++

c++20

In C++20, is there a way to annotate a ternary expression with [[likely]]/[[unlikely]] to hint the compiler which of the two outcomes is more likely?

The following syntax doesn't seem to work

condition ? [[likely]] function1() : function2()

Is there a different syntax to annotate ternary expressions? Or will I have to use an if instead?

like image 681
Vogelsgesang Avatar asked Jul 29 '21 22:07

Vogelsgesang


People also ask

How do you write 3 conditions in a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Which symbol is used for ternary operator?

In computer programming, ?: is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.

Which is the correct example of a ternary operator?

Example: C Ternary Operator Here, age >= 18 - test condition that checks if input value is greater or equal to 18. printf("You can vote") - expression1 that is executed if condition is true. printf("You cannot vote") - expression2 that is executed if condition is false.

How do I use the ternary conditional operator in query strings?

This guide will show you how to use the ternary conditional operator, also known as ternary-if, in query strings. The ternary conditional operator evaluates a boolean expression and returns the result of one of two expressions, depending on whether the boolean expression evaluates to true or false.

How do you use the ternary operator?

We can use the ternary operator to create a new table with a "yes" or "no" column that answers if a wood type is within the budget constraints. For more complex cases with multiple conditions, ternary-if statements can be nested.

How to avoid writing deeply nested complex ternary conditionals?

Day by day, high-level languages are introducing more features so that developers could write more human-readable expressive code. We should keep this point in our mind and avoid a few things like writing deeply nested complex ternary conditionals. Instead, go for conventional control flow statements like if-else or if-return-early.

Can a ternary operator replace an if..else statement?

A ternary operator can be used to replace an if..else statement in certain situations. Before you learn about ternary operators, be sure to check the JavaScript if...else tutorial. What is a Ternary operator? A ternary operator evaluates a condition and executes a block of code based on the condition.


Video Answer


1 Answers

Is there a different syntax to annotate ternary expressions?

No, there is no way to annotate a ternary conditional expression with the [[likely]] attribute.

Or will I have to use an if instead?

Yes. Or alternatively, you can omit the attribute.


For what it's worth, it is possible using the GNU extension:

__builtin_expect(!!condition, 1) ? function1() : function2()
like image 75
eerorika Avatar answered Oct 25 '22 09:10

eerorika