Inheritance of things that are note instance methods

Arafel via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 3 06:02:06 PDT 2017


On 08/03/2017 02:28 PM, Adam D. Ruppe wrote:
> 
> There's no place in the binary for it to pass it that information. With 
> a non-static member, `this` is passed as a hidden function argument. 
> With static, there explicitly are no hidden function arguments (it is 
> compatible with outside function pointers). The template can do it since 
> it creates several copies... and as such is NOT compatible with function 
> pointers.
> 
> The function pointer compatibility is fairly important.
> 

Well, here I agree, that's why I was suggesting the "templated this" 
approach. I think it'd fit nicely. It would be a template and thus 
create as many copies as needed, meaning that if you have:

ยดยดยด
class A {
	public:
	static C getInstance(this C)() {
		// Do some stuff here
		return new C();
	}
	private:
	this() { }
}
class B : A { }
```

&A.foo and &B.foo would point (as reasonable and expected, on the other 
hand) to different functions, and they would of course return different 
types. It would also follow the principle of least astonishment.

> This has to do with the overload rules, see the hijack article linked 
> above.

Well, the article only talked about functions and such, I didn't see any 
reference to how it would apply to templates.

Still, fair enough, it looks reasonable, since one can always explicitly 
dispatch to the parent class if needed... a bit cumbersome, but doable.

Thanks for the explanation!

A.


More information about the Digitalmars-d mailing list