On 12/02/2016 13:02, Peter Dimov wrote:
Gavin Lambert wrote:
The only reason that shared_ptr::operator* does not throw is that the class author decided that this is likely a hot path and the calling code has *probably* already checked for null, so it is more *efficient* to omit the check entirely (and cause undefined behavior if called in violation of that assumption).
That's not true. The class author decided that calling operator* on a NULL pointer is a logic error and therefore a correct program should never do so. This doesn't have anything to do with performance; it's a question of design.
Then why doesn't it throw a logic_error? Or call abort()? Again, an assert is a useful tool in debug mode but it disappears in release mode. The only reason to elide this check is for performance. Much as it might be considered "bad practice", there *is* software that is only ever built and tested in release mode (or at least only rarely in debug mode). For this software, the assert might as well not be there -- because it isn't.