Friday, August 8, 2014

Ternary Operator

Recently we had a discussion on  Conditionals Performance i.e. with ternary operator and if else

Still I am not sure  which one is best at this point but found something while surfing .

The ternary operator shouldn't differ in performance from a well-written equivalent if/else statement... they may well resolve to the same representation in the Abstract Syntax Tree, undergo the same optimization etc..

Things you can only do with ? :

If you're initializing a constant or reference, or working out which value to use inside a member initialization list, then if/else statements can't be used but ? : can be:
const int x = f() ? 10 : 2;

The above scenario we can only implement with ternary and off course we can try with if but might end in a DRY.

0 comments:

Post a Comment