www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why not arbitrary template value parameters?


at the moment, template value parameters, like T in this example:

template foo(int T)

can only have an integral type (here: int). This is very inconvenient. I think, 
it would be better to allow arbitrary types (or at least pointers).

For the template specialization (which may become more complicated then) it 
would be a simple solution to ignore these non-integral value parameters (there 
may be problems with "identity or equality" for class instances as parameters).

A practical usage example would be setting/getting of array elements, where the 
array is automatically expanded as necessary. The problem is the default value 
of not yet set array items, which may be different from the .init value of the 
item type.

The getter could then look like this:

template arrget(T, T DEFAULT = T.init)
  T arrget(T[] arr, uint index)
    if (arr.length < index + 1)
      return DEFAULT;
    return arr[index];

Currently this only works if T is an integral type.

May 23 2005