3 ms·
I don't really see how one could make it a real no-op, and an error would imply that pop_back would look like if(empty()) { /* some error handling mechanis
by jcelerier 4y ago
I don't really see how one could make it a real no-op, and an error would imply that pop_back would look like
if(empty()) { /* some error handling mechanism */ }
... rest of the code ...
and every added branch adds some minimal but non zero cost
- tialaramex 4y agoHowever really often this is a case you actually care about, and so if you don't capture it in pop you end up asking a predicate. I have a whole bunch of Rust code that does: while let Some(thing) = container.pop() { /* ... */ } There is indeed a branch, that pop function might return None - but when it does our while loop exits, the container is empty and we're done. My guess is that the idiomatic C++ here checks !empty() for the loop and then calls pop() on the container if it's not empty so in these cases we're doing the same work except we made it more error prone.