www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - convenient backward compatible template arguments type deduction


  a template function foo(T) (T[] a, T[] b) { .. }

It is being called using:

dmd will fail because it can't deduce the type. So far so expected.

Now, there are situations where I don't care in my template whether any  
part is mutable or not (so I would want use const)
and there are situations where certain parameters need to be mutable.

One solution I found:
  foo(T,U=T) (T[] a, U[] b) { .. }

Problem is, I still can't say which one should be const, if any.  
Additionally, the types can be very different
and I should add checks for that.

The obvious solution
   foo(T) (const(T)[] a, const(T)[] b) { .. }

is not available to me in this case, because I want it to be D1 compatible.
I tried to do this:

template Constify(T)
		mixin("alias const(T)	Constify;");
		alias T Constify;
foo(T) (Constify!(T)[] a, Constify!(T)[] b) { .. }

but this fails at deducing the type from the arguments.
I could of course provide either a version for D1 and one for D2 or just  
mixin the body of the function,
but I was hoping someone here knows a better way.

Oct 10 2010