Partial Specialization using Static If

Kirk McDonald kirklin.mcdonald at gmail.com
Sun Dec 24 21:47:01 PST 2006


Xinok wrote:
> Going back to my 'multiple specialization' idea...
> 
> Static if can pretty much do the job of multiple specialization, except for
> one case: partial specialization.
> 
> template temp(T : T*)
> 
> This doesn't work in a static if:
> static if(is(T == T*))
> Assuming T = int, the compiler would read this as:
> static if(is(int == int*))
> 

But you can say this:

static if (is(T U == U*))

U is then declared to be an alias of the type T is a pointer to (and it 
only exists within the static if's scope).

Assuming T is int*, you get

static if (is(int* U == int*))
     static assert(is(U == int));

This works more generally, too:

static if (is(T U == U[]))

That determines if T is a dynamic array, and U becomes an alias for the 
type of an element of the array.

> 
> I'll forget about the multiple specialization idea for now. We need a simple
> way to do partial specialization in a static if expression. It is possible to
> do using a specialized template, but it's a bit of work.
> 
> template IsPtr(T){
> 	const bool IsPtr = false;
> }
> template IsPtr(T : T*){
> 	const bool IsPtr = true;
> }
> 
> template temp(T1, T2, T3){
> 	static if(IsPtr!(T1) || IsPtr!(T2) || IsPtr(T3)){
> 	}
> }


-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d mailing list