2 ms·
Because alternative makes it not much better or safer. It could throw an exception when vector is empty (or return an error in whatever other way) - you will ca
by menaerus 4y ago
Because alternative makes it not much better or safer. It could throw an exception when vector is empty (or return an error in whatever other way) - you will catch the exception and then do what, try to pop_back() again or what exactly can you do? You are at very bad odds to continue with the program when such a fundamental issue pops up in your program so I guess you can only terminate the process?
There is no generally meaningful way to recover from an error condition like this, which in its nature is a logical mistake in your code. And that's the reason why I think std::vector won't throw an exception but will give you an opt-in to terminate the process through the assert.
I think it's not about the performance, although it can be argued, but it's more fundamental code design problem.
- twic 4y ago> or what exactly can you do? Crash. Crashing is a million times better than undefined behaviour, which can and often does include carrying on computing with incorrect data.
- menaerus 4y agoAnd which is what I already mentioned? And something that standard library already can do for you so I'm not sure about the point you're trying to make. Also, aborting the program is not something you can universally apply. For some it works well for others not so much. Most recent example being the rust in the kernel which, well, tries to take this sort of a purist approach, however, real world doesn't work that way.