C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 29 Oct 2024 16:22:55 +0000
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.

Received on 2024-10-29 16:23:08