Template namespaces - Obsolete?

BCS BCS at pathlink.com
Wed Sep 19 14:36:56 PDT 2007


Michel Fortin wrote:
> On 2007-09-19 15:23:36 -0400, "Janice Caron" <caron800 at googlemail.com> 
> said:
> 
>> Is there any need, any more, for the old fashioned
>>
>> template t(T)
>> {
>>     int f();
>>     int g();
>> }
> 
> 
> I haven't seen it yet, but templates are very powerful when combined 
> with static if. You can only do that the "old fashioned" way.
> 
> This code is from the D/Objective-C bridge I released yesterday:
> 
> /**
> * Number of arguments in method name. This simply count the number of
> * colon character within the string.
> */
> template methodArgumentCount(char[] name) {
>     static if (name.length == 0) {
>         const uint methodArgumentCount = 0;
>     } else static if (name[0] == ':') {
>         const uint methodArgumentCount = 1 + 
> methodArgumentCount!(name[1..$]);
>     } else {
>         const uint methodArgumentCount = methodArgumentCount!(name[1..$]);
>     }
> }
> 
> 

That reminds me of another uses for the full form

template Foo(bool b)
{
	static if(b)
		alias T!(int, float, char) args;
	else
		alias T!(short byte) args;

	void Bar(args val){}
}


Iv never actually had code where I needed that, but I came close. What I 
ended up with was this:

enum Ver
{
	zero = 0,
	one = 1,
	two = 2
}

T!(byte, short, int)[t] Foo(Ver t)(T!(int, short, byte)[0..t] args){}

I was just lucky enough that the order of the args was the same for all 
cases, just some of them omitted the later ones. (That was some nasty code!)



More information about the Digitalmars-d mailing list