[Issue 12510] New: Templated overload ignored
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Apr 3 00:26:39 PDT 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12510
Summary: Templated overload ignored
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: regression
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: jmdavisProg at gmx.com
--- Comment #0 from Jonathan M Davis <jmdavisProg at gmx.com> 2014-04-03 00:26:37 PDT ---
This code results in infinite recursion:
class C
{
bool foo(C rhs)
{
return false;
}
}
bool foo(T, U)(T lhs, U rhs)
if (is(T == class) && is(U == class) &&
is(typeof(lhs.foo(rhs)) == bool) &&
is(typeof(rhs.foo(lhs)) == bool))
{
return true;
}
bool foo()(const C lhs, const C rhs)
{
return foo(cast()lhs, cast()rhs);
}
void main()
{
const C a = new C;
const C b = new C;
auto d = foo(a, b);
}
And whether a and b are const or not doesn't matter. The first overload is
simply never called. It always calls the second overload. And making it so that
the second overload isn't templatized has no effect. It used to be that this
would compile, because it was working in this pull request:
https://github.com/D-Programming-Language/druntime/pull/459
whereas that pull request now results in infinite recursion.
I'd have to try it on several older versions of dmd though to figure out when
it broke, but this definitely worked before. My first guess is that it broke
when it was fixed so that templated and non-template functions could be
overloaded.
The fact that the first overload fails with const objects should force them to
use the second overload (because C.foo isn't const), whereas if they're not
const, then the first should match better than the second. And to make matters
worse, if I change the code to
class C
{
bool foo(C rhs)
{
return false;
}
}
bool foo(T, U)(T lhs, U rhs)
{
return true;
}
bool foo()(const C lhs, const C rhs)
{
return foo(cast()lhs, cast()rhs);
}
void main()
{
const C a = new C;
const C b = new C;
auto d = foo(a, b);
}
it still compiles with the same result, whereas it _should_ result in a
compilation error due to ambiguity, because both functions match equally well.
Instead, the second function is always called, whether it's templatized or not
and whether the arguments are const or not.
--
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list