Ternary operator ?: vs if...else

Xirdus picture Xirdus · Aug 25, 2010 · Viewed 108.6k times · Source

In C++, is the ?: operator faster than if()...else statements? Are there any differences between them in compiled code?

Answer

Kirill V. Lyadvinsky picture Kirill V. Lyadvinsky · Aug 25, 2010

It is not faster. There is one difference when you can initialize a constant variable depending on some expression:

const int x = (a<b) ? b : a;

You can't do the same with if-else.