Best way to refer to the type of a struct inside itself ?

Paul Backus snarwin at gmail.com
Fri May 15 15:04:45 UTC 2020


On Friday, 15 May 2020 at 14:55:07 UTC, Ali Çehreli wrote:
> Additionally, the name of a template when used inside that 
> template means that instance of it. So just say Foo. :)
>
> struct Foo(A, B, C, size_t a, size_t b)
> {
>   Foo * p;
> }
>
> Ali

To expand a little, this works because a struct template such as 
the one above is actually syntax sugar for the following:

template Foo(A, B, C, size_t a, size_t b)
{
     struct Foo
     {
         // refers to the inner `struct Foo`, not the outer 
`template Foo`
         Foo* p;
     }
}

The relevant parts of the language spec are:

- Aggregate Templates: 
https://dlang.org/spec/template.html#aggregate_templates
- Eponymous Templates: 
https://dlang.org/spec/template.html#implicit_template_properties


More information about the Digitalmars-d-learn mailing list