Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 28, 2013 detect anonymous union at compile time | ||||
---|---|---|---|---|
| ||||
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). |
March 28, 2013 Re: detect anonymous union at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | 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. |
March 28, 2013 Re: detect anonymous union at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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.
|
March 28, 2013 Re: detect anonymous union at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 prints #{overlap...} for unions. I can use that code, which works regardless of protection. Thanks
|
March 29, 2013 Re: detect anonymous union at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On 3/28/13, cal <callumenator@gmail.com> wrote:
> Just realized that format.d is able to figure it out, since it prints #{overlap...} for unions. I can use that code, which works regardless of protection. Thanks
Ah, I didn't even know .tupleof would contain .offsetof.
void main()
{
static if (S.tupleof[0].offsetof == S.tupleof[1].offsetof)
pragma(msg, "Union");
}
|
Copyright © 1999-2021 by the D Language Foundation