Fixing C's Biggest Mistake

kdevel kdevel at vogtner.de
Sun Jan 1 19:14:24 UTC 2023


On Sunday, 1 January 2023 at 18:11:10 UTC, Walter Bright wrote:
> On 1/1/2023 7:24 AM, Sebastiaan Koppe wrote:
>> Also, somewhat tangentially related, isn't nonnull-ness one of 
>> the things `ref` helps with:
>
> Sadly, not completely:
>
>   void foo(ref int);
>
>   void test()
>   {
>     int* p = null;
>     foo(*p);
>   }
>
> (C++ has the same issue.)

How? Quote from https://eel.is/c++draft/dcl.ref:

    [...] A reference shall be initialized to refer to a valid 
object or function.

    [Note 2: In particular, a null reference cannot exist in a 
well-defined
    program, because the only way to create such a reference would 
be to bind it
    to the “object” obtained by indirection through a null 
pointer, which causes
    undefined behavior. {...} — end note]



More information about the Digitalmars-d mailing list