Strange template instantiating behaviour

Era Scarecrow rtcvb32 at yahoo.com
Sat Jan 12 12:59:08 PST 2013


On Saturday, 12 January 2013 at 19:15:37 UTC, ref2401 wrote:
> public template rgba(uint value) {
> 	enum Color rgba = Color(
> 		cast(ubyte)((value >> 24) & 0xFF),
> 		cast(ubyte)((value >> 16) & 0xFF),
> 		cast(ubyte)((value >> 8) & 0xFF),
> 		cast(ubyte)(value & 0xFF));
> }
>
> public struct Color {
> 	public static immutable Color black = rgba!0xFF;
>
> 	public ubyte r = 0;
> 	public ubyte g = 0;
> 	public ubyte b = 0;
> 	public ubyte a = 0;
>
>
> 	public this(ubyte r, ubyte g, ubyte b, ubyte a) {
> 		this.r = r;
> 		this.g = g;
> 		this.b = b;
> 		this.a = a;
> 	}

> Now, if we change fields order in the struct we will get 
> strange template instantiating behaviour.
>
> public struct Color {
> 	public static immutable Color black = rgba!0xFF;
>       // "a" is the last field in the previous example
> 	public ubyte a = 0;
>
> 	public ubyte r = 0;
> 	public ubyte g = 0;
> 	public ubyte b = 0;
>
<snip>
> Console output:
> [template result] (r: 0, g: 0, b: 255, a: 0)
> [ctor. result] (r: 0, g: 0, b: 0, a: 255)
>
> if we change templateValue variable or Color.black value(or 
> even comment it) all works correct. is it a bug?

  Your ctor still is accepting arguments in rgba order (vs argb) 
while your template says clearly the alpha channel is the lowest 
8 bytes (the a in the first instance, or b in the last); Thereby 
from the looks of it, it is working correctly. If you remove the 
constructor entirely, then both of them should act the same, as 
then it's based on byte order (but a/alpha won't be the last one).


More information about the Digitalmars-d-learn mailing list