Search code examples
c++language-lawyerreturn-typestdseterase-remove-idiom

Why does `std::set::erase(const key_type&)` return `size_type` instead of `bool`?


Because std::set doesn't insert() duplicates, it's assured to contain unique elements. When using the overload erase(const key_type&), its object would have contained maximum 1 element of that same value. Hence, it may return either 1 (if present) or 0 (otherwise).

When can erase(const key_type&) may return more than 1?
In other words, what is the purpose of returning a size_type instead of simple bool?


Solution

  • The purpose of returning size_type is consistency; all associative containers have an erase method that takes a key type and returns a size. Even if the size can only be zero or one, it's still the same interface.