Why doesn't std::shared_ptr dereference throw a null pointer exception (or similar)?

前端 未结 2 1057
鱼传尺愫
鱼传尺愫 2021-02-20 06:47

Exceptions are a big part of C++ and one of the reasons to use it (I know there are many, more important, other reasons) is to avoid needless checks that obfuscate code with a l

2条回答
  •  旧巷少年郎
    2021-02-20 07:15

    If every dereference of a shared pointer was required to check for nullptr and conditionally throw an exception, there could be a lot of redundant checks, code bloat and overhead. Sure - the optimiser's likely to eliminate some of that, but still.... Instead, the programmer's expected to check once before however-many dereferences.

提交回复
热议问题