NonNull template

kdevel kdevel at vogtner.de
Sat Apr 19 11:26:29 UTC 2025


On Saturday, 19 April 2025 at 10:35:01 UTC, Jonathan M Davis 
wrote:
> [...] because then it would need to dereference s to access its 
> i member, but until it needs to access a member, there's no 
> reason for any dereferencing to take place.
>
> The same happens with C++ classes as long as the function isn't 
> virtual.

That is undefined behavior. In the C++ standard null references 
have been carefully ruled out [1]. There is no standard 
conforming C++ program having null references.

> And with
>
> int* p = null;
> ref r = *p;
>
> no dereferencing occurs,

In C++ this is a programming error. When creating a reference from
a pointer the null check it is necessary in order to uphold C++' 
guarantee
that references are actually bound to existing objects.

[1] google.com?q="c++ reference from null pointer"
     - 
https://old.reddit.com/r/cpp/comments/80zm83/no_references_are_never_null/
     - 
https://stackoverflow.com/questions/4364536/is-a-null-reference-possible



More information about the Digitalmars-d mailing list