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

前端 未结 2 1045
鱼传尺愫
鱼传尺愫 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.

    0 讨论(0)
  • 2021-02-20 07:22

    My understanding is that the smart pointer classes are designed to look and behave like raw pointers. Given this guiding design principal, ideally legacy code could simply replace usage of raw pointers with smart pointers using equivalent ownership semantics and the code would work exactly as before.

    Therefore, changing the behavior for dereferencing smart pointers should not do any additional checks or throw exceptions (i.e., since raw pointers don't behave this way).

    The proposal to add smart pointers to the standard indicates this design decision (A Proposal to Add General Purpose Smart Pointers to the Library Technical Report):

    III. Design Decisions

    A. General Principles

    1. "As Close to Raw Pointers as Possible, but no Closer"
    0 讨论(0)
提交回复
热议问题