digitalmars.D - Partial Specialization using Static If
- Xinok (21/21) Dec 24 2006 Going back to my 'multiple specialization' idea...
- Kirk McDonald (16/44) Dec 24 2006 But you can say this:
- Xinok (1/12) Dec 24 2006 Thanks, I didn't know you can do that.
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*)) 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)){ } }
Dec 24 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
Dec 24 2006
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.Thanks, I didn't know you can do that.
Dec 24 2006