C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Standardising 0xdeadbeef for pointers

From: David Brown <david.brown_at_[hidden]>
Date: Sun, 3 Aug 2025 18:15:58 +0200
On 03/08/2025 16:48, Frederick Virchanza Gotham via Std-Proposals wrote:
>
>
> I'm going off on a tangent here, but imagine if we could mark a function
> (or lambda) as follows:
>
> int Func(double) throw_on_nullptr
> {
> . . .
> }
>
> If you try to dereference a nullptr in this function, or if you perform
> addition or subtraction on a nullptr, it throws std::nullptr_t.
>
> Maybe?
>

What use would that be? If the programmer knows that a pointer in the
function might be a null pointer, and wants to throw if that is the
case, then the programmer can write "if (!p) throw nullptr;", or
whatever suits their needs.

Since dereferencing a null pointer is UB, then it's fine for a compiler
implementation to have flags causing particular behaviour on such
attempts - that's what you get with sanitisers.

Received on 2025-08-03 16:16:06