Dean Song
Dean Song

Reputation: 371

Why use if-else rather than ternary operation in std::max implementation

In the implementation of std::max of in Ubuntu, I am confused as to why they use an if statement rather than the ternary operation. The ternary operation is in the comment (so it looks like it once was that way and was changed). Any specify reason to use if-else here?

template<typename _Tp>
inline const _Tp&
max(const _Tp& __a, const _Tp& __b)
{
  // concept requirements
  __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  //return  __a < __b ? __b : __a;
  if (__a < __b)
    return __b;
  return __a;
}

Upvotes: 3

Views: 218

Answers (1)

Mike Robinson
Mike Robinson

Reputation: 8945

I daresay that, "when the compiler's object-code generator gets through with either human-syntax alternative," it will make no difference whatsoever.

During the ordinary process of "compilation," 'your intentions' will be reduced to a series of primitive operations which will then be translated (after optimization ...) into executable code.   If, as in this case, "there are several ways to say it in source-code," they will most-likely be reduced to only one.

Upvotes: 2

Related Questions