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, 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?

The reason I am asking is that if I add ?: support for vector types (à la OpenCL), with "if" std::max will still fail on vectors, but with ?: it would suddenly become accepted.

Hmm, I should have waited until I had at least a rough prototype to speak. std::max returns a reference to one of its arguments. For vectors, ?: produces an rvalue (it is an operator similar to & or |, except that it takes 3 arguments). So we have replaced code that failed to compile with code that (silently in my prototype) returns a reference to a temporary :-( Seems like if I want a std::max for vectors, I need to overload it...


--
Marc Glisse


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