- A+

C++ Templates - The Complete Guide, 2nd Edition introduces the max template:

`template<typename T> T max (T a, T b) { // if b < a then yield a else yield b return b < a ? a : b; } `

And it explains using `“b < a ? a : b”`

instead of `“a < b ? b : a”`

:

Note that the max() template according to [StepanovNotes] intentionally returns “b < a ? a : b” instead of “a < b ? b : a” to ensure that the function behaves correctly even if the two values are equivalent but not equal.

How to understand "`even if the two values are equivalent but not equal.`

"? `“a < b ? b : a”`

seems have the same result for me.

The standard defines `std::max(a, b)`

as follows [alg.min.max] (emphasis is mine):

`template<class T> constexpr const T& max(const T& a, const T& b);`

Requires: Type T is LessThanComparable (Table 18).

Returns: The larger value.

Remarks:Returns the first argument when the arguments are equivalent.

Equivalent here means that `!(a < b) && !(b < a)`

is `true`

[alg.sorting#5].

In particular, if `a`

and `b`

are equivalent, both `a < b`

and `b < a`

are `false`

, so the value on the right of `:`

will be returned in the conditional operator, so `a`

has to be on the right, so:

`a < b ? b : a `

...seems to be the correct answer. This is the version used by libstdc++ and libc++.

So the information in your quote seems wrong to me...