Template method and type resolution of return type

matovitch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 20 01:35:06 PDT 2014


On Sunday, 20 April 2014 at 08:28:07 UTC, monarch_dodra wrote:
> On Sunday, 20 April 2014 at 07:52:08 UTC, matovitch wrote:
>>
>> struct S
>> {
>>   ubyte get()  { return 0 ; }
>>   float get()  { return 0.; }
>> }
>>
>> void main()
>> {
>>   S s;
>>   float x = s.get();  // does'nt know which overload, does'nt 
>> compile.
>> }
>
> What I do find interesting though, is that you are allowed to 
> write the overload, whereas C++ would outright block you for 
> ambiguity "at the source".
>
> This means that with proper meta magic eg 
> `__traits(getOverloadSet, S, "get")`, you could, *manually* 
> resolve the ambiguity yourself.

You mean getOverloads ? (yes it's interesting)

How about this :

class S
{
	public
	{
		this() {}

		union other {
			SFloat u_float;
			SUbyte u_ubyte;
		}

		alias other this;
	}
}

struct SFloat
{
	float data;
	alias data this;
}

struct SUbyte
{
	ubyte data;
	alias data this;
}

void main()
{
    S s;
    s.other.u_float.data = 0.5;
    //float x = s;
}

This gives :

main.d(31): Error: need 'this' for 'data' of type 'float'



More information about the Digitalmars-d-learn mailing list