Deduction regression or improvement?

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 9 09:06:00 PST 2016


On Wed, Mar 09, 2016 at 02:42:51PM +0000, Meta via Digitalmars-d wrote:
> On Tuesday, 8 March 2016 at 22:35:57 UTC, H. S. Teoh wrote:
> >IMO, this *should* compile and infer T == const(SomeStruct) as the
> >common type of a and b.  But I'm not sure whether or not this is a
> >regression.
> 
> Does template type inference do implicit conversion? I thought it did
> not, and thus would expect this example to not compile.
[...]

Hmm. I tried a little test, which revealed something interesting:

	class A {}
	class B : A {}
	class C : A {}
	
	void func(T)(T a, T b) {}
	
	void main() {
		auto x = new A;
		auto y = new B;
		auto z = new C;
	
		func(x,y);	// OK
		func(x,z);	// OK
		func(y,z);	// compile error
	
		func(y,x);	// OK
		func(z,x);	// OK
		func(z,y);	// compile error
	}

It seems like *some* kind of implicit conversion is happening, because
the compiler is able to figure out, in the case where one of the
arguments is the base class, that it should convert the other argument
to the base class.

However, it doesn't seem to be able to figure out the case where neither
argument is the base class, even though both does share the same base
class.  Arguably, this could be construed to be a bug / enhancement
request?

It does seem to imply that IFTI does include some form of implicit
conversion check, though.


T

-- 
If you think you are too small to make a difference, try sleeping in a closed room with a mosquito. -- Jan van Steenbergen


More information about the Digitalmars-d mailing list