Template unique IDs per type

Roderick Gibson kniteli at gmail.com
Thu Jul 11 21:55:35 PDT 2013


On Friday, 12 July 2013 at 04:42:28 UTC, Jonathan M Davis wrote:
> Now, it's probably possible to use static constructors to use 
> global mutable
> state to intialize all of those IDs when the program starts up, 
> which would
> initialize the IDs to incrementing values, so they could be 
> immutable if you
> wanted, but that would still be at runtime.
>
> - Jonathan M Davis

As a follow up, here's what it looks like, and it works perfectly:

alias size_t component_type;

class Component(T) : BaseComponent {
	static immutable component_type component_type_id;

	static this() {
		component_type_id = component_type_counter++;
	}
}

class BaseComponent {
	static component_type component_type_counter;
}



More information about the Digitalmars-d-learn mailing list