[Issue 5896] New: const overload with Template function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 27 01:58:52 PDT 2011


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

           Summary: const overload with Template function
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: k.hara.pg at gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg at gmail.com> 2011-04-27 01:54:51 PDT ---
This code doesn't compile when -version=expect.
----
struct X
{
  version(expect)
  {
    T opCast(T)()      { return 10; }  // 1a
    T opCast(T)()const { return 11; }  // 1b
  }
  else
  {
    T opCast(T:int)()      { return 10; }  // 2a
    T opCast(T:int)()const { return 11; }  // 2b
  }
}
void main()
{
  auto xm = X();
  auto xc = const(X)();
  assert(cast(int)xm == 10);
  assert(cast(int)xc == 11);
}
----
test.d(18): Error: template test.X.opCast(T) opCast(T) matches more than one
template declaration, test.d(5):opCast(T) and test.d(6):opCast(T)
----

Template function matching processed by template.c
TemplateDeclaration::deduceFunctionTemplateMatch(), and
const overload matching is more priority than template parameter one.

T:int(2a,2b) is more specialized from T(1a,1b), then first priority is
MATCHexact and second is MATCHconvert.

Next, currently const overload matching returns MATCHconst or not, but the
priority of T(MATCHconvert) is less than MATCHconst, so both 1a and 1b return
MATCHconvert, then make ambiguous.

It seems to me that more priority level is need:
Priority name
0 MATCHnomatch
1 MATCHconvertconst  ... const conversion + template parameter conversion (new)
2 MATCHconvert       ... template parameter conversion
3 MATCHconst         ... const conversion
4 MATCHexact         ... exact match

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list