Misunderstanding, silly error, or compiler bug?

Regan Heath regan at netmail.co.nz
Fri Aug 10 07:26:33 PDT 2007


Peter C. Chapin wrote:
> I think I answered my own question on this. I now understand that == and
> != do value comparisons. Thus even though the right parameter is a
> reference, the operator is intended to access the object pointed at by
> that reference to render its decision. Thus something like
> 
> 	current == null
> 
> is doomed to failure even if 'current' refers to a real object because
> opEqual is going to want to dereference null.

That particular case depends on the opEquals implementation, eg.

import std.stdio;

class A
{
	int i;
	
	int opEquals(A rhs)
	{
		//return i == rhs.i;
		if (rhs) return i == rhs.i;
		return 0;
	}
}

void main()
{
	A a = new A();
	A b = null;
	
	if (a == b) writefln("equal");
	else writefln("not");
}

If opEquals was implemented as commented it would crash, if it was 
implemented as written it wouldn't.

I wouldn't rely on opEquals being written in a non-crashing way, after 
all even if it were the statement:

if (b == a)

will always crash.

Regan


More information about the Digitalmars-d-learn mailing list