News for hackers. Stuff that matters.
Re:The joy of max() by andrepd on Friday March 30, 2018 @08:34PM (#16720940)
This in cpp:

    template<class T> 
    constexpr const T& max(const T& a, const T& b) {
        return a>b ? a : b;
    }
Re:The joy of max() by mehrdadn on Friday March 30, 2018 @09:14PM (#16721077)
Edit: apologies, I'm blind; never mind. I thought I double-checked but the use of > instead of < tripped me up.
Re:The joy of max() by darkmx0z on Friday March 30, 2018 @09:57PM (#16721222)
Stepanov disagrees. You should return the second parameter if a, b are equivalent (and you should use < anyway):

  template<typename T>
  inline constexpr const T& max(const T& a, const T& b) {
     return (b < a ? a : b);
  }
Re:The joy of max() by a_t48 on Saturday March 31, 2018 @03:27AM (#16722221)
What’s the reasoning behind this?
Re:The joy of max() by enitihas on Saturday March 31, 2018 @03:41AM (#16722252)
I think to be comparable in c++ types are required to implement < operator and the same is exclusively used by the stdlib.
Re:The joy of max() by darkmx0z on Saturday March 31, 2018 @11:41AM (#16723720)
If you sort2(a, b), it would be nice if min(a, b) == a && max(a, b) == b.
Re:The joy of max() by loeg on Friday March 30, 2018 @11:25PM (#16721511)
The C version works with distinct types for a and b. Does this?
Re:The joy of max() by Jyaif on Saturday March 31, 2018 @02:25AM (#16722043)
That's not a feature, it's more bug prone.
Re:The joy of max() by andrepd on Saturday March 31, 2018 @05:48AM (#16722597)
This one works too, if they are implicitly convertible (e.g. max(1, 2.0))
Re:The joy of max() by bstamour on Saturday March 31, 2018 @08:10AM (#16722957)
No it won't. You'll get a compile time error, as T could either deduce to int or double. Unless you explicitly specify it as max<int>(1, 2.0) or max<double>(1, 2.0).