digitalmars.D.learn - detect anonymous union at compile time
- cal (10/10) Mar 28 2013 Given:
- Andrej Mitrovic (7/11) Mar 28 2013 void main()
- cal (5/6) Mar 28 2013 Yeh this is the problem. I can map data layout of complex
- cal (4/6) Mar 28 2013 Just realized that format.d is able to figure it out, since it
- Andrej Mitrovic (7/10) Mar 28 2013 Ah, I didn't even know .tupleof would contain .offsetof.
Given: struct S { union { int i; float f; } } is there any way to detect the fact that fields i and f will have the same offset from S? (Creating an instance of S and getting the relative addresses only works if the union is public).
Mar 28 2013
On Thursday, 28 March 2013 at 20:02:18 UTC, cal wrote:is there any way to detect the fact that fields i and f will have the same offset from S?void main() { static if (S.init.i.offsetof == S.init.f.offsetof) pragma(msg, "Union"); }(Creating an instance of S and getting the relative addresses only works if the union is public)..offsetof will also require access rights to the fields.
Mar 28 2013
On Thursday, 28 March 2013 at 20:18:45 UTC, Andrej Mitrovic wrote:.offsetof will also require access rights to the fields.Yeh this is the problem. I can map data layout of complex aggregates, even if the members are private, but unions mess that up. Restricting to public fields is an option, but i'd like to be able to get it all.
Mar 28 2013
On Thursday, 28 March 2013 at 20:18:45 UTC, Andrej Mitrovic wrote:On Thursday, 28 March 2013 at 20:02:18 UTC, cal wrote: .offsetof will also require access rights to the fields.Just realized that format.d is able to figure it out, since it regardless of protection. Thanks
Mar 28 2013
On 3/28/13, cal <callumenator gmail.com> wrote:Just realized that format.d is able to figure it out, since it regardless of protection. ThanksAh, I didn't even know .tupleof would contain .offsetof. void main() { static if (S.tupleof[0].offsetof == S.tupleof[1].offsetof) pragma(msg, "Union"); }
Mar 28 2013