Is this actually supposed to be legal?
Timon Gehr
timon.gehr at gmx.ch
Tue Jul 17 14:11:43 PDT 2012
On 07/17/2012 10:50 PM, Jonathan M Davis wrote:
> On Tuesday, July 17, 2012 22:36:10 Timon Gehr wrote:
>> On 07/17/2012 07:23 PM, Jonathan M Davis wrote:
>>> On Tuesday, July 17, 2012 14:48:32 David Nadlinger wrote:
>>>> On Tuesday, 17 July 2012 at 05:24:26 UTC, Jonathan M Davis wrote:
>>>>> This code strikes me as being a bug:
>>>>>
>>>>> --------
>>>>> class MyBase(T)
>>>>> {}
>>>>>
>>>>> class MySubA : MyBase!MySubA
>>>>> {}
>>>>>
>>>>> class MySubB : MyBase!MySubB
>>>>> {}
>>>>>
>>>>> void main()
>>>>> {}
>>>>> --------
>>>>
>>>> This pattern is actually quite common in C++ code, and referred
>>>> to as CRTP (curiously recurring template pattern). If you propose
>>>> to kill it, Andrei is going to get mad at you. ;)
>>>
>>> Well, it certainly seems insane to me at first glance - particularly when
>>> you take compile time reflection into account, since the derived classes'
>>> definitions are now effectively recursive (so I suspect that the
>>> situation is worse in D, since C++ doesn't have conditional compliation
>>> like D does).
>> The fact that it is allowed does not make the compiler's job
>> significantly more complicated. It is not important if the type is
>> passed as a template argument or referred to directly from inside the
>> template -- the issues are the same.
>
> The problem is that if you have static ifs and the like in the base class
> which depends on compile time reflection of the derived class, you effectively
> have a recursive template definition. e.g.
>
> class MyBase(T)
> {
> static if(is(typeof(T.func())))
> {
> int func() { return 42; }
> }
> }
>
> - Jonathan M Davis
This issue is unrelated to CRTP. (also, you probably want to negate
that static if condition, otherwise the code is valid and poses no
challenge to a compiler.)
class MyBase{
static if(!is(typeof(T.func())))
int func() { return 42; }
}
class T : MyBase { }
More information about the Digitalmars-d-learn
mailing list