digitalmars.D.learn - trait detecting anonymous union?
- Bastiaan Veelo (23/23) May 22 2017 `
 - Stanislav Blinov (50/73) May 22 2017 There isn't a built-in one. The best I can muster at 1AM is
 - Bastiaan Veelo (2/8) May 23 2017 Good idea, thanks for staying up while I was asleep :-)
 - Vladimir Panteleev (3/5) May 22 2017 I have an implementation here:
 - Bastiaan Veelo (3/8) May 23 2017 Interesting work. Thanks or sharing!
 
`
void main()
{
	import std.stdio;
	struct S
	{
		int i;
		union
		{
			int a;
                         double b;
		}
	}
	S s;
	import std.traits;
	writeln([FieldNameTuple!S]); // ["i", "a", "b"]
}
`
Is there a way to detect at CT that S has overlapping data 
members, when an anonimous union is used as above?
Thanks,
Bastiaan.
 May 22 2017
On Monday, 22 May 2017 at 21:03:42 UTC, Bastiaan Veelo wrote:
 `
 void main()
 {
 	import std.stdio;
 	struct S
 	{
 		int i;
 		union
 		{
 			int a;
                         double b;
 		}
 	}
 	S s;
 	import std.traits;
 	writeln([FieldNameTuple!S]); // ["i", "a", "b"]
 }
 `
 Is there a way to detect at CT that S has overlapping data 
 members, when an anonimous union is used as above?
 Thanks,
 Bastiaan.
There isn't a built-in one. The best I can muster at 1AM is 
finding all fields that have the same offset:
size_t[] memberOffsetsOf(T)()
{
     size_t[] result;
     foreach(i, _; typeof(T.tupleof))
         result ~= T.tupleof[i].offsetof;
     return result;
}
string[] overlappingFieldsOf(T)()
{
     import std.traits : FieldNameTuple;
     import std.range : array, enumerate;
     import std.algorithm : map, filter, count;
     enum offsets = memberOffsetsOf!T;
     auto names = [FieldNameTuple!T];
     bool isOverlapping(size_t i) {
         return offsets.count(offsets[i]) > 1;
     }
     return names
            .enumerate
            .filter!(a => isOverlapping(a.index))
            .map!(a => a.value)
            .array;
}
void main()
{
	import std.stdio;
	struct S
	{
		int i;
		union
		{
			int a;
             double b;
		}
         int j;
         union
         {
             struct { short n, m; }
             float k;
         }
	}
	S s;
     writeln(overlappingFieldsOf!S); // ["a", "b", "n", "k"]
}
which is not quite the same as full overlap. This looks like a 
fun exercise, considering fields can also have arbitrary 
alignment...
 May 22 2017
On Monday, 22 May 2017 at 22:11:15 UTC, Stanislav Blinov wrote:On Monday, 22 May 2017 at 21:03:42 UTC, Bastiaan Veelo wrote:Good idea, thanks for staying up while I was asleep :-)Is there a way to detect at CT that S has overlapping data members, when an anonimous union is used as above?There isn't a built-in one. The best I can muster at 1AM is finding all fields that have the same offset:
 May 23 2017
On Monday, 22 May 2017 at 21:03:42 UTC, Bastiaan Veelo wrote:Is there a way to detect at CT that S has overlapping data members, when an anonimous union is used as above?I have an implementation here: https://github.com/CyberShadow/rclidasm/blob/31bde3347ec1259026b6ab15e2305f2a99e63a30/src/rclidasm/meta.d#L110-L183
 May 22 2017
On Tuesday, 23 May 2017 at 01:02:59 UTC, Vladimir Panteleev wrote:On Monday, 22 May 2017 at 21:03:42 UTC, Bastiaan Veelo wrote:Interesting work. Thanks or sharing! Bastiaan.Is there a way to detect at CT that S has overlapping data members, when an anonimous union is used as above?I have an implementation here: https://github.com/CyberShadow/rclidasm/blob/31bde3347ec1259026b6ab15e2305f2a99e63a30/src/rclidasm/meta.d#L110-L183
 May 23 2017








 
 
 
 Bastiaan Veelo <Bastiaan Veelo.net> 