[Issue 1049] New: Template specialization via delegate parameters

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 10 06:31:04 PST 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1049

           Summary: Template specialization via delegate parameters
           Product: D
           Version: 1.007
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: gavrilyak at gmail.com


If template function has delegate argument the type of this argument is not
used when deducting the right function to use.
Compiler gives error about conflicting templates
At the same time compiler refuses to accept one such template instead of
another, so it looks like 2 different templates to it.

Example

T[] filter (T) (T[] me, bool delegate (T) predicate)  {
  T[] result;
  foreach (it; me)
    if (predicate(it))
      result ~= it;
  return result;
}

T[] filter (T) (T[] me, bool delegate (T,int) predicate)  {
  T[] result;
  foreach (i, it; me)
    if (predicate(it, i))
      result ~= it;
  return result;
}

void main(){
   int[] arr   = [1,2,3,4];
   auto gt2    = filter(arr, (int it){return it>3;});
   auto first2 = filter(arr, (int it,int i){return i<2;});
}

This code produces compiler error
>>template tdel.filter(T) conflicts with tdel.filter(T) at tdel.d(1)

Changing first filter to _filter give another error
tdel.d(19): template tdel.filter(T) does not match any template declaration
tdel.d(19): template tdel.filter(T) cannot deduce template function from
argument types (int[],bool delegate(int it))


So it seems compiler knows that functions are different and cannot use one
instead of another, so there is no conflict here.


-- 



More information about the Digitalmars-d-bugs mailing list