[Issue 1644] New: Template instantiation should automatically cast to const to make const-ness irrelevant when argument is const anyways
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Nov 7 08:30:48 PST 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1644
Summary: Template instantiation should automatically cast to
const to make const-ness irrelevant when argument is
const anyways
Product: D
Version: 2.007
Platform: All
OS/Version: All
Status: NEW
Keywords: spec
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: schveiguy at yahoo.com
In the current definition of const, an argument to a function that takes a
const type can implicitly cast a mutable or invariant value to const in order
to call the function. However, for implicit function template instantiation,
the compiler treats const, invariant, and mutable types as completely separate.
So for example, a function like:
void foo(T)(const(T)[] arg1, const(T)[] arg2){...}
This could be for instance a string function, but is templated to allow for
char, wchar, and dchar. However, ifti will not figure out how to instantiate
if the two arguments differ by const-ness. For example, this line fails:
foo("hello", "world".dup);
So my proposal is that if the compiler sees that a template function parameter
specifies that the argument is const, when doing ifti, it should cast that
argument to const before trying to instantiate. So the above call becomes:
foo(cast(const)"hello", cast(const)"world".dup);
And now the compiler can match the type (T = char). This also has the very
beneficial side effect of only instantiating one template per type, no matter
what const value is passed in. For example, in the current compiler, the
following three lines generate 3 different identical implementations. But in
the new scheme, they only generate one.
foo("hello", "world");
foo("hello".dup, "world".dup);
foo(cast(const)"hello", cast(const)"world");
--
More information about the Digitalmars-d-bugs
mailing list