D casting broke?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 21 05:11:03 PDT 2016


On 6/20/16 10:45 PM, Joerg Joergonson wrote:
> On Monday, 20 June 2016 at 23:35:28 UTC, Steven Schveighoffer wrote:
>> On 6/19/16 5:19 PM, Joerg Joergonson wrote:
>>> On Sunday, 19 June 2016 at 20:21:35 UTC, ag0aep6g wrote:
>>>> On 06/19/2016 09:59 PM, Joerg Joergonson wrote:
>>>>> This should be completely valid since B!T' obviously derives from A!T
>>>>> directly
>>>>
>>>> ok
>>>>
>>>>> and we see that T' derives from b which derives from a
>>>>> directly.
>>>>
>>>> ok
>>>>
>>>>> So B!b is an entirely derived from A!a
>>>>
>>>> No. B!b is derived from A!b, not from A!a. `b` being derived from `a`
>>>> does not make A!b derived from A!a.
>>>
>>> why not? This doesn't seem logical!
>>
>> Because:
>>
>> class A(T : a)
>> {
>>   static if(is(T == a))
>>      int oops;
>>   ...
>> }
>>
>> Now A!b and A!a have different layouts. They cannot be related, even
>> if the template arguments are related. I could introduce another
>> virtual function inside the static if, same result -- vtable is messed
>> up.
>>
>> In general, an instantiation of a template aggregate (class or struct)
>> is not castable implicitly to another instantiation of the same
>> aggregate unless explicitly declared.
>>
>> And note that D does not allow multiple inheritance. I don't think you
>> can solve this problem in D.
>>
>
> Yes, but all you guys are doing is leaving out what I'm actually doing
> and creating a different problem that may not have the same issues.

We're not "creating" any different problems. The compiler has to assume 
the worst, especially when it must make assumptions at runtime. The same 
template instantiated with different parameters is different, not 
related. In order for it to be related, you have to declare that somehow.

-Steve


More information about the Digitalmars-d-learn mailing list