Static Analysis at Mozilla, @must_override

Michel Fortin michel.fortin at michelf.com
Mon Jul 12 08:57:11 PDT 2010


On 2010-07-12 10:36:05 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> On 07/12/2010 07:15 AM, bearophile wrote:
>> A video, Large-Scale Static Analysis of C++ code at Mozilla, video, 
>> July 9th, 2010:
>> http://vimeo.com/12614626
>> 
>> 
>>  From the video I have seen that Mozilla developers have felt the need 
>> to add this new attribute in C++ (implemented in JavaScript through 
>> their hydra plug-ins and used with a define NS_MUST_OVERRIDE):
>> http://mxr.mozilla.org/mozilla-central/source/xpcom/analysis/must-override.js
> 
> I discussed this with Walter. The pattern is of pretty narrow
> usefulness, but the scenarios are quite compelling:
> 
> - cloning
> 
> - opCmp
> 
> - opEquals
> 
> Such functions, if ever used, must be overridden transitively.

Not necessarily. The basic implementations of opCmp and opEqual in 
Object are quite good for a variety of cases (when comparing the 
reference pointer is good enough). You only need to override them when 
two different instances having the same value makes sense.

As for cloning, if we had a little more runtime reflection it'd be 
easily doable like this:

	T clone(T)(T o) {
		ClassInfo ci = o.classinfo;
		if (ci.hasConstructorWithArguments!(ref const(T))) {
			return ci.construct(o);
		}
		throw new Exception("No copy constructor");
	}

Don't try to make that compile, it won't work. But it could be made to...

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list