C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Calling methods on a nullptr

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Mon, 28 Oct 2024 07:57:58 +0100
On 27/10/2024 23.04, Sebastian Wittmeier via Std-Proposals wrote:
> Hi Jens,
>
> if one can (in theory) call member functions on a nullptr, then those are either not using the this-object or are static, as there is no usable object.
>
> (Any other interpretation like having a default object would be too far-fetched and can be achieved by just creating a normal object and use it by default.)
>
> 315 brought up both cases: The non-static member function as example and the static member function in the text.
>
> As far as I understand the non-static member function case was explicitly UB in the standard, but the static member function case was unclear, as although nullptr->staticf() leads to (*nullptr).staticf(), the left side of which was definitely evaluated, but not necessarily dereferenced.

It was dereferenced, because *nullptr was evaluated, and that led to the
text I quoted.

> In 315 there is an older resolution that (even?) the shown (non-static?) example is valid.

CWG opted not to go down this path, which would have been a change from the status quo.

> 2823 made it clear that (*nullptr) is always UB.

What would the result of *nullptr be, with the previous wording?

Jens

Received on 2024-10-28 06:58:02