NonNull template

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Apr 20 22:19:39 UTC 2025


On Sunday, April 20, 2025 8:13:44 AM MDT kdevel via Digitalmars-d wrote:
> On Sunday, 20 April 2025 at 00:33:52 UTC, Jonathan M Davis wrote:
> > I see no practical difference in general [...]
>
> I consider nonconforming generally inacceptable.

Writing a program which doesn't behave properly is always a problem and
should be consider unacceptable. And both having a program relying on
undefined behavior and having a program which dereferences null are
problems. The latter will crash the program. The only real difference there
between C++ and D is that if the language states that it's undefined
behavior for the reference to be null, then the optimizer can do screwy
things in the case when it actually does happen instead of the program being
guaranteed to crash when the null reference is dereferenced.

So, that's why I say that I see no practical difference. If you create a
reference from a null pointer, you have a bug whether the program is written
in C++ or D. And outside of optimized builds (and likely in almost all cases
even with optimized builds), what happens when you screw that up will be the
same in both languages.

In any case, we clearly both agree that if the programmer does this, they've
screwed up, and I think that we're basically arguing over language here
rather than an actual technical problem.

Ultimately, the only real difference is what the language's optimizer is
allowed to do when the programmer does screw it up, because the C++ spec
says that it's undefined behavior, and the D spec can't say that and have
references work in @safe code, since @safe code disallows undefined behavior
in order to ensure memory safety. The program has a bug either way.

- Jonathan M Davis





More information about the Digitalmars-d mailing list