3 ms·
But what if I want to name the inner struct so I can refer to Outer::Inner (for example, for use with sizeof or similar, or to create a temporary to hold a copy
by addaon 4y ago
But what if I want to name the inner struct so I can refer to Outer::Inner (for example, for use with sizeof or similar, or to create a temporary to hold a copy of that type) later? Do I need to use typeof(Outer::inner)? And then of course multiple members of the same type...
- lerno 4y agoYou can't name the inner struct. So here are the options: Say that you want this from C: struct Foo { struct Bar { int x; int y; } bar; }; struct Foo f; f.bar = (struct Bar) { .x = 123 }; The struct in C3: struct Foo { struct bar { int x; int y; } }; Foo f; It is correct that we don't get the inner type, but because C3 has inference for {} assignment can happen despite that: f.bar = { .x = 123 }; // Valid in C3 You can grab the type using typeof if you want, but interestingly there's also the possibility to use structural casts, which are valid in C3: struct HelperStruct { int x; int y; } HelperStruct test = (HelperStruct)f.bar; // structural cast But the normal way would be to create a named struct outside if you really need a named struct, e.g.: struct Bar { int x, y; } struct Foo { Bar bar; };
- mst 4y agoThat ... actually seems reasonable? If you need it to be first class visible outside, you declare it outside. I believe I could live with this.