www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Order of object fields

reply frame <frame86 live.com> writes:
Is the order of fields guaranteed returned by `.tupleof` and 
`__traits(getMember,...)`, can I rely on this? I know that are 
different things, I mean just per each use case if I have more 
functions that traverses through all fields. Thx.
Jan 27 2022
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Thursday, 27 January 2022 at 12:45:20 UTC, frame wrote:
 Is the order of fields guaranteed returned by `.tupleof` and 
 `__traits(getMember,...)`, can I rely on this? I know that are 
 different things, I mean just per each use case if I have more 
 functions that traverses through all fields. Thx.
Not for classes. The compiler is free to reorder if it wants to. Yes for structs. -Steve
Jan 27 2022
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/27/22 8:13 AM, Steven Schveighoffer wrote:
 On Thursday, 27 January 2022 at 12:45:20 UTC, frame wrote:
 Is the order of fields guaranteed returned by `.tupleof` and 
 `__traits(getMember,...)`, can I rely on this? I know that are 
 different things, I mean just per each use case if I have more 
 functions that traverses through all fields. Thx.
Not for classes. The compiler is free to reorder if it wants to. Yes for structs.
To clarify, the compiler can reorder the members to fit them better. But it will be the same order for any particular build of the object files. Any compile-time features that fetch the members should be in the same order as the compiler has decided to lay them out. It's just that it might not match the source code order. -Steve
Jan 27 2022