Is this actually supposed to be legal?

Timon Gehr timon.gehr at gmx.ch
Wed Jul 18 15:09:44 PDT 2012


On 07/18/2012 11:08 PM, monarch_dodra wrote:
> On Tuesday, 17 July 2012 at 23:38:04 UTC, Jonathan M Davis wrote:
>> It's not that it makes the compiler's life hard. It's the fact that
>> conditional compilation relies on state that doesn't exist yet. It's
>> messed up
>> to be checking whether an object defines something when you're in the
>> middle of
>> defining that object.
>>
>> [snip]
>>
>> - Jonathan M Davis
>
> Well, while you "can" do it in C++ as the "Curiously Recursive Template
> Pattern" (particularly popular way of implementing the singleton pattern
> BTW), you can't just do anything you feel like doing with it.
>
> If I remember correctly, in C++, you can't access any of T's members, or
> create any (stack) instances of T, or (I think) call T's any of T's
> static members, because "T is not correctly formed yet".
>
> Did you try anything more advanced? For example, this outright _crashes_
> my (r)dmd:
>
> --------
> class MyBase(T)
> {
>     int a = T.hello();
> }
>
> class MySubA : MyBase!MySubA
> {
>     static int hello(){return 0;}
> }
> --------

Well, that is a bug.

> I'm not entirely sure how valid the comparison with C++'s CRTP is,
> because D's classes are actually pointer to implementation,  but I think
> it is a safe bet that what C++ can't do, neither can D.

Careful there. D allows forward references. This is all supposed to work 
in D. (but DMD is poor when it comes to tricky symbol lookup tasks.)


More information about the Digitalmars-d-learn mailing list