Archived

This topic is now archived and is closed to further replies.

Peaceman

Ternary operators

Recommended Posts

Not faster, but sometimes easier to read/understand. Stuff like checking for the max of two numbers (a > b ? a : b), that sorta thing works wonders for readability. But using it in place of a general if-then statement can make code harder to read as well. My suggestion: use them if you''re confortable with them, and preferably only when they improve readability/ease of understanding of the code''s purpose.

Share this post


Link to post
Share on other sites
quote:
Original post by Melraidin
Not faster, but sometimes easier to read/understand.



If your CPU architecture supports predicated operations, then it may be faster, as it is 'more obvious' to the compiler what to do with it.

Be aware of the constraints of the operator though. For instance, the expression (bool)b:‌(double)d?(long)l is always of type double.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 7, 2002 11:36:33 AM]

Share this post


Link to post
Share on other sites