Reputation: 51
I'm not sure, but I think I've read that you should only delete a vector if it's not empty. So now I always check before I delete a vector:
if (!vector.empty()) {
vector.clear();
}
However, I no longer find anything about whether this step is unnecessary.
Is it forbidden to use clear
on an empty vector?
Upvotes: 3
Views: 331
Reputation: 12263
Indeed, there was an issue related to the undefined behavior while clearing out an empty sequences or associative containers but this is fixed now and it is perfectly safe to call std::vector::clear
without previously checking if the std::vector
is empty.
Upvotes: 3