Search code examples
c++unique-ptrdereferencenoexcept

Why is unique_ptr operator* not noexcept?


While implementing a basic std library for my hobby OS I came across this and wondered why:

Both operator->() and T* get() are marked as noexcept, however operator*() is not. According to the reference it should be equivalent to *get(), which would allow it to be noexcept and looking at some implementations I see no reason why it is not.

Why is unique_ptr's dereferencing operator not marked as noexcept?


Solution

  • From cppreference

    • typename std::add_lvalue_reference<T>::type operator*() const; (1) (since C++11)
    • pointer operator->() const noexcept; (2) (since C++11)

    And then:

    Exceptions:
    1) may throw, e.g. if pointer defines a throwing operator*