www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Best way to refer to the type of a struct inside itself ?

reply wjoe <invalid example.com> writes:
struct Foo(A, B, C, size_t a, size_t b)
{
   alias foo_t = Foo!(A, B, C, a, b); // is there a better way to 
get foo_t ?
}
May 15 2020
parent reply Paul Backus <snarwin gmail.com> writes:
On Friday, 15 May 2020 at 13:47:43 UTC, wjoe wrote:
 struct Foo(A, B, C, size_t a, size_t b)
 {
   alias foo_t = Foo!(A, B, C, a, b); // is there a better way 
 to get foo_t ?
 }
typeof(this)
May 15 2020
parent reply wjoe <invalid example.com> writes:
On Friday, 15 May 2020 at 13:52:38 UTC, Paul Backus wrote:
 On Friday, 15 May 2020 at 13:47:43 UTC, wjoe wrote:
 struct Foo(A, B, C, size_t a, size_t b)
 {
   alias foo_t = Foo!(A, B, C, a, b); // is there a better way 
 to get foo_t ?
 }
typeof(this)
Thanks :)
May 15 2020
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/15/20 7:37 AM, wjoe wrote:
 On Friday, 15 May 2020 at 13:52:38 UTC, Paul Backus wrote:
 On Friday, 15 May 2020 at 13:47:43 UTC, wjoe wrote:
 struct Foo(A, B, C, size_t a, size_t b)
 {
 =C2=A0 alias foo_t =3D Foo!(A, B, C, a, b); // is there a better way =
to get=20
 foo_t ?
 }
typeof(this)
=20 Thanks :)
Additionally, the name of a template when used inside that template=20 means that instance of it. So just say Foo. :) struct Foo(A, B, C, size_t a, size_t b) { Foo * p; } Ali
May 15 2020
parent reply Paul Backus <snarwin gmail.com> writes:
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
May 15 2020
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/15/20 8:04 AM, Paul Backus wrote:
 On Friday, 15 May 2020 at 14:55:07 UTC, Ali =C3=87ehreli wrote:
 Additionally, the name of a template when used inside that template=20
 means that instance of it. So just say Foo. :)

 struct Foo(A, B, C, size_t a, size_t b)
 {
 =C2=A0 Foo * p;
 }

 Ali
=20 To expand a little, this works because a struct template such as the on=
e=20
 above is actually syntax sugar for the following:
=20
 template Foo(A, B, C, size_t a, size_t b)
 {
  =C2=A0=C2=A0=C2=A0 struct Foo
  =C2=A0=C2=A0=C2=A0 {
  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // refers to the inner `str=
uct Foo`, not the outer `template Foo`
  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Foo* p;
  =C2=A0=C2=A0=C2=A0 }
 }
=20
 The relevant parts of the language spec are:
=20
 - Aggregate Templates:=20
 https://dlang.org/spec/template.html#aggregate_templates
 - Eponymous Templates:=20
 https://dlang.org/spec/template.html#implicit_template_properties
Yes, that is a consistent way of explaining it. :) As an off-topic trivia, the same feature is in C++ as well: #include <iostream> template <class A, class B, class C, size_t a, size_t b> struct Foo { Foo * p; // <-- Foo means the template instance }; int main() { Foo<int, double, long, 10, 20> f; f.p =3D &f; } Ali
May 15 2020
parent wjoe <invalid example.com> writes:
On Friday, 15 May 2020 at 15:24:32 UTC, Ali Çehreli wrote:
 On 5/15/20 8:04 AM, Paul Backus wrote:
 [...]
Yes, that is a consistent way of explaining it. :) As an off-topic trivia, the same feature is in C++ as well: [...] Ali
Awesome. Thank you Ali and Paul :)
May 15 2020