Define an order for allMembers traits

Timon Gehr timon.gehr at gmx.ch
Wed Aug 23 21:17:34 UTC 2023


On 8/23/23 18:28, Andrey Zherikov wrote:
> On Wednesday, 23 August 2023 at 15:32:02 UTC, Paul Backus wrote:
>> For this use case, you should use `.tupleof` instead of 
>> `__traits(allMembers)`, which [is guaranteed by the spec to give the 
>> fields in declaration order][1].
>>
>> [1]: https://dlang.org/spec/class.html#class_properties
> 
> This link says "The order of the fields in the tuple matches the order 
> in which the fields are declared" for the classes. I don't see such 
> statement for structs 
> [here](https://dlang.org/spec/struct.html#struct_instance_properties).

Well, it refers to the class version and I think it is clear that it 
must be ordered. Anyway, if you don't trust this for some reason, you 
can always just sort by "offsetof". (Provided you don't want to use 
empty static arrays.)


More information about the Digitalmars-d mailing list