[Issue 4576] New: accepts-invalid: 0/1 argument calls to overloaded function is allowed in presence of variadic function
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Aug 3 12:27:17 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4576
Summary: accepts-invalid: 0/1 argument calls to overloaded
function is allowed in presence of variadic function
Product: D
Version: D2
Platform: Other
OS/Version: Windows
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: andrej.mitrovich at gmail.com
--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2010-08-03 12:27:15 PDT ---
According to TDPL, this should be an ambiguous call and issue a compiler error:
import std.algorithm, std.array, std.stdio;
@property bool empty(T)(T[] a) { return a.length == 0; }
@property ref T front(T)(T[] a) { return a[0]; }
void popFront(T)(ref T[] a) { a = a[1 .. $]; }
V reduce(alias fun, V, R)(V x, R range)
{
for ( ; !range.empty; range.popFront()) {
x = fun(x, range.front);
}
return x;
}
double average() { return 2.0; }
double average(double) { return 3.0; }
// compute the avg of a set of numbers, passable directly or via an array
double average(double[] values...)
{
if (values.empty)
{
throw new Exception("Average of zero elements is undefined");
}
return reduce!((a, b) { return a + b; })(0.0, values) / values.length;
}
unittest {
average(); // This should not compile, but does
writeln(average()); // calls average(), returns 2.0
writeln(average(0)); // calls average(double), returns 3.0
assert(average(0) == 0); // fails, since it calls average() which returns
2.0
assert(average(1, 2) == 1.5);
assert(average(1, 2, 3) == 2);
// passing arrays and slices works too
double[] v = [1, 2, 3];
assert(average(v) == 2);
}
void main() { }
--
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