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");
Comment #1 by schveiguy — 2008-07-10T13:44:49Z
I'm changing this to critical, because Tango cannot be compiled with D2 without either this or the scoped const feature (#1961).
Comment #2 by bugzilla — 2008-08-13T17:21:10Z
*** Bug 2204 has been marked as a duplicate of this bug. ***