Pascal Menuet (6) [Avatar] Offline
#1
In the code snippet for the implementation of operator==, we could avoid testing left.is_sentinel() possibly twice, by nesting the ifs:
if (left.is_sentinel()) {
    if (right.is_sentinel()) {
        return true;
    } else {
        // ...
    }
} else if (right.is_sentinel()) {
        // ...
} else {
        // ...
}
Ivan Cukic (94) [Avatar] Offline
#2
Yes, if this was real code, that change would be sensible. Though, most of the time, things like is_sentinel are inlined (in template classes) by the compiler, and it can optimize the branching.

For the book, the snippet server just to demonstrate the point that there are 4 options for each call of the operator== and this layout makes it more readable than having nested if statements.