microsoft / microsoft/STL

`<memory>`: `!_Get_ptr()` in the destructors of `inout_ptr_t` and `out_ptr_t` _should_ be correct

Open
#4,964 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

blocked on LWG
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug

Currently !_Get_ptr() is used in the destructors of inout_ptr_t and out_ptr_t.

https://github.com/microsoft/STL/blob/a26f4adb1ecf767784bb4e4318093c4579fdb364/stl/inc/memory#L4409-L4411
https://github.com/microsoft/STL/blob/a26f4adb1ecf767784bb4e4318093c4579fdb364/stl/inc/memory#L4328-L4330

However, it seems that nothing in Cpp17NullablePointer ([nullablepointer.requirements]) requires !p to be semantically equivalent to p != nullptr or even to be well-formed. E.g. a type with a deleted member operator! (the NoExclamation type shown below) can still meet the Cpp17NullablePointer requirements.

#include <memory>

struct NoExclamation {
    using pointer = NoExclamation;

    void* p_{};

    NoExclamation() = default;
    constexpr NoExclamation(decltype(nullptr)) noexcept {}

    friend bool operator==(NoExclamation, NoExclamation) = default;
    friend constexpr bool operator==(NoExclamation lhs, decltype(nullptr)) noexcept
    {
        return lhs.p_ == nullptr;
    }

    constexpr explicit operator bool() const noexcept
    {
        return p_ != nullptr;
    }

    void operator!() const = delete; // HERE!
};

int main()
{
    NoExclamation ne{};
    std::out_ptr(ne);
}

Currently no implementation accepts the example due to the deleted operator!.

Command-line test case

Godbolt link

Expected behavior

Perhaps the program should compile.

STL version

a26f4adb1ecf767784bb4e4318093c4579fdb364 (probably every version after #1998)

Additional context

As libc++ and libstdc++ also expect usable operator!, should there be an LWG issue to make current implementation strategies comforming?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the destructor implementations in stl/inc/memory at the linked lines for inout_ptr_t and out_ptr_t, then compare their requirements with Cpp17NullablePointer. Use the NoExclamation example and the linked Godbolt case to assess the behavior; done means determining whether the implementation or the standard wording needs to change.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.