Strange template instantiating behaviour

ref2401 refactor24 at gmail.com
Sat Jan 12 11:15:36 PST 2013


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;
	}

	public string toString() const
	{
		return std.string.format("(r: %s, g: %s, b: %s, a: %s)", r, g, 
b, a);
	}
}

void main()
{
	Color templateValue = rgba!0x00_00_00_FF;
	Color ctorValue = Color(0, 0, 0, 255);

	writefln("[template result] %s", templateValue);
	writefln("[ctor. result] %s", ctorValue);
	readln();
}

Console output:
[template result] (r: 0, g: 0, b: 0, a: 255)
[ctor. result] (r: 0, g: 0, b: 0, a: 255)


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;


	public ubyte a = 0; // "a" is the last field in the previous 
example

	public ubyte r = 0;

	public ubyte g = 0;

	public ubyte b = 0;

	...

}

...

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?


More information about the Digitalmars-d-learn mailing list