CTFE Assignment to anonymous union shows unexpected behavior

Rekel paultjeadriaanse at gmail.com
Thu Apr 22 22:47:17 UTC 2021


I'm not sure why this is happening, but after simplifying my code 
I traced it back to what the title may suggest. The original 
cause of my issues being summarized by debug print statements 
returning a union as:
> Mat([nanf, nanF, . . . .], [[1.0F, 0.0F, . . . .])
Even though the nanF should thus be 1.0, 0.0, etc...

This is example code that describes when this happens:

```d
import std.stdio;

struct Apple(uint size) {
	union {
		int[size * size] a;
		int[size][size] b;
	}

	static immutable typeof(this) pie = _pie();
	private static typeof(this) _pie() pure {
		typeof(this) result;
		static foreach (i; 0 .. size)
			static foreach (j; 0 .. size)
				//result.a[i + j * size] = 1; // Works
				result.b[i][j] = 1; // Fails
		return result;
	}
}

void main() {
	Apple!(4) a = Apple!(4).pie;
	writeln(a.a);
	writeln(a.b);
}
```

The working code changes the first integers to 1, the failing 
version keeps them at 0.

What's the reason for this? Logically this doesn't seem 
troublesome to me, and if assigning to non-initial anonymous 
union varialbes isn't possible(?!) that would be pretty bad, and 
I'd be in for quite some trouble in my actual code :(


More information about the Digitalmars-d-learn mailing list