Is this actually supposed to be legal?

monarch_dodra monarchdodra at gmail.com
Wed Jul 18 14:08:50 PDT 2012


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;}
}
--------
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.


More information about the Digitalmars-d-learn mailing list