Typeinfo.compare and opCmp woes

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Jul 7 22:13:59 PDT 2013


On Sun, Jul 07, 2013 at 09:44:59PM -0700, H. S. Teoh wrote:
> I have a partial fix for issues 8435 and 10118, but it's being blocked
> by issue 10567. Basically, the problem is that opCmp *must* be
> declared as:
> 
> 	struct T {
> 		int opCmp(ref const T t) const { ... }
> 	}
> 
> Only when it's declared this way, will T's typeinfo.compare pick up the
> correct custom opCmp.
[...]

Upon further investigation (TypeInfoStructDeclaration::toDt in DMD), it
appears that the reason for this restriction is that typeinfo.compare is
implemented as a call to a func ptr of signature:

	int function(in void*, in void*) xopCmp
	
the first argument of which is T.this, and the second of which must be
ABI-compatible with the argument to opCmp. Basically, it looks like
opCmp is simply cast into the appropriate func ptr and stored in
typeinfo.xopCmp.

This is unfortunate, because it means opCmp declared in any other way
cannot possibly be put into the typeinfo, unless DMD generates a wrapper
function that conforms to the above function signature.

So basically if opCmp is a template function, we're screwed, since due
to the bug that we can't have both a template and non-template method of
the same name, it's impossible to declare a non-template opCmp with the
correct signature. Then typeinfo.compare will always be wrong.


T

-- 
Unix was not designed to stop people from doing stupid things, because that would also stop them from doing clever things. -- Doug Gwyn


More information about the Digitalmars-d mailing list