NonNull template
kdevel
kdevel at vogtner.de
Sat Apr 19 12:13:36 UTC 2025
On Saturday, 19 April 2025 at 11:44:42 UTC, Jonathan M Davis
wrote:
> [...]
>> > 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.
>> [...]
>
> Well, if C++ now checks that pointer is non-null when creating
> a reference from it, that's new behavior, because it most
> definitely did not do that before.
Of course it doesn't and I didn't write that. I wrote that it is
a programming error to use a ptr to initialize a reference when
it is possible that the ptr is null. If refs in D were as strong
as in C++ I would write
[... int *p is potentially null ...]
enforce (p);
auto ref r = *p;
More information about the Digitalmars-d
mailing list