This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: std::max: if versus ?:


On Thu, 20 Sep 2012, Paolo Carlini wrote:

On 09/20/2012 01:55 PM, Marc Glisse wrote:
Hello,

I noticed that std::max is implemented as:

      //return  __a < __b ? __b : __a;
      if (__a < __b)
        return __b;
      return __a;

Notice how it doesn't use the ?: code but the "if" version instead. Looking at the history, this seems to come from SGI. Does someone know why it was done this way?
I'm going to tell you something very vague and non technical, but in the past, outside std::max, I had quite often to use the above instead of a conditional operator as a second thought, when (maybe on a less common target) a test "unexpectedly" failed with a conversion error. Does that help you figuring out the technical details of the issue?

When a and b have possibly different types, using "if" makes a lot of sense (I even advocated replacing ?: with "if" somewhere in the gcc codebase in PR50177). Inside std::max, a and b both have exactly the same type const _Tp&, so it shouldn't matter.


Note that libcxx uses ?: for its implementation of std::max.

I assume there are historical reasons, some compilers had bugs in their ?: support or produced slightly faster code with "if"...

--
Marc Glisse


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]