Thread overview
[Issue 1644] New: Template instantiation should automatically cast to const to make const-ness irrelevant when argument is const anyways
Nov 07, 2007
d-bugmail
Jul 10, 2008
d-bugmail
Aug 13, 2008
d-bugmail
Aug 14, 2008
d-bugmail
November 07, 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@digitalmars.com
        ReportedBy: schveiguy@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");


-- 

July 10, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1644


schveiguy@yahoo.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|enhancement                 |critical




------- Comment #1 from schveiguy@yahoo.com  2008-07-10 13:44 -------
I'm changing this to critical, because Tango cannot be compiled with D2 without either this or the scoped const feature (#1961).


-- 

August 13, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1644





------- Comment #2 from bugzilla@digitalmars.com  2008-08-13 17:21 -------
*** Bug 2204 has been marked as a duplicate of this bug. ***


-- 

August 14, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1644


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #3 from bugzilla@digitalmars.com  2008-08-14 02:59 -------
Fixed dmd 2.018


--