www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - other array types than char in templates?

reply dennis luehring <dl.soluz gmx.net> writes:
what is the reason for allowing this

template test(char[] bla)
{
...
}


but not

template test(int[] bla)
{
...
}

or other bultin(own) types?

ciao dennis
May 29 2007
next sibling parent BCS <BCS pathlink.com> writes:
dennis luehring wrote:
 what is the reason for allowing this
 
 template test(char[] bla)
 {
 ....
 }
 
 
 but not
 
 template test(int[] bla)
 {
 ....
 }
 
 or other bultin(own) types?
 
 ciao dennis

I'm with you on this. see this: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=53939
May 29 2007
prev sibling next sibling parent Reiner Pope <some address.com> writes:
dennis luehring wrote:
 what is the reason for allowing this
 
 template test(char[] bla)
 {
 ...
 }
 
 
 but not
 
 template test(int[] bla)
 {
 ...
 }
 
 or other bultin(own) types?
 
 ciao dennis

I agree that it doesn't make sense, but you can do this hack: template test(T...) { static assert(is(typeof(T[0]) == int[])); const int[] bla = T[0]; } -- Reiner
May 29 2007
prev sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
dennis luehring wrote:
 what is the reason for allowing this
 
 template test(char[] bla)
 {
 ....
 }
 
 
 but not
 
 template test(int[] bla)
 {
 ....
 }
 
 or other bultin(own) types?
 
 ciao dennis

I think it's because arguments to a template become part of its mangled name. Integers and floats are easy, as are actual strings, but to support arbitrary types, Walter would either need some kind of generic serialisation setup, or find a new way of mangling templates. At least, that's how I understand it :) -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 29 2007
parent Don Clugston <dac nospam.com.au> writes:
Daniel Keep wrote:
 
 dennis luehring wrote:
 what is the reason for allowing this

 template test(char[] bla)
 {
 ....
 }


 but not

 template test(int[] bla)
 {
 ....
 }

 or other bultin(own) types?

 ciao dennis

I think it's because arguments to a template become part of its mangled name.

  Integers and floats are easy, as are actual strings, but to
 support arbitrary types, Walter would either need some kind of generic
 serialisation setup, or find a new way of mangling templates.

That's not much of an issue. In fact, if you look at the ABI page, you'll see that the mangling scheme almost supports arbitrary types already. TemplateArg: T Type V Type Value S LName Any type which can be a type template parameter, and which has literals, could also be a value parameter. The problem is that the mangled names become too long, which makes bad things happen (eg, the obj file format imposes a maximum identifier length of ~ 3kB). There a few types (int[], short[] ubyte[]) which are essentially identical to the permitted dchar[], wchar[], char[], and could trivially be permitted. CTFE has drastically reduced the number of cases where it would be useful, though.
May 29 2007