Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return from the max function Stepanov Notes [duplicate]

Tags:

c++

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

like image 656
Hariom Singh Avatar asked Nov 08 '22 16:11

Hariom Singh


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

like image 200
James Maa Avatar answered Nov 15 '22 11:11

James Maa