Is this actually supposed to be legal?
Jonathan M Davis
jmdavisProg at gmx.com
Tue Jul 17 13:50:14 PDT 2012
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(tyepeof(T.func())))
{
int func() { return 42; }
}
}
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list