C++ Logo

std-proposals

Advanced search

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

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Tue, 29 Oct 2024 12:01:40 -0500
> I'm proposing a change to the language.

Do you have a concrete example of a problem that could be uniquely solved
by being able to call a member function on a null pointer? My gut feeling
is that this is an anti-pattern and symptom of bad design, however, I might
not be considering all possible use cases.

Cheers,
Jeremy

On Tue, Oct 29, 2024 at 11:23 Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tue, Oct 29, 2024 at 2:56 PM Andre Kostur wrote:
> >
> > p->fn(); says that I want to invoke fn() on the object where p is
> pointing.
>
>
> Yeah I agree, I would assume that decltype(*p) is a class that has a
> member function called 'fn'. Or maybe decltype(*p) is a C-style struct
> that contains a member variable which is a function pointer.
>
>
> > But if p is nullptr, then it is not pointing at a valid object, so that
> statement does not make semantical sense.
>
>
> Yeah I agree; currently in C++23, invoking a method on a nullptr
> doesn't make sense.
>
>
> > That one can concoct some mechanism that it will do something is rather
> beside the point.
>
>
> I'm proposing a change to the language.
>
>
> > Additionally, what happens if fn() is a pure virtual function? Yay,
> more UB, just harder to find?
>
>
> In my original post, I suggested a syntax such as:
>
> class MyClass friend nullptr { ... };
>
> to indicate to the compiler that you can invoke methods on a nullptr
> for this class. And so if 'MyClass' contains any methods lacking an
> implementation, the compiler shall terminate compilation and issue a
> diagnostic.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-10-29 17:01:53