Return from the max function Stepanov Notes

Viewed 443

max() template according to Stepanov Notes intentionally return

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 Few explanations over here but still couldn't understand http://stepanovpapers.com/notes.pdf (page 63)

I am not able to think of a use case when two values will be equivalent but not equal

1 Answers

when a==b first returns b, second returns a

This would triggers when you overload a class's < function

e.g.

class myClass {
public:
    int key;
    string value;
    bool operator<(const myClass& rhs) {
        return this->key < rhs.key;
    }
}

You may need to decide what do you want your program do in a more specific case

Related