www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1644] New: Template instantiation should automatically cast to const to make const-ness irrelevant when argument is const anyways

reply d-bugmail puremagic.com writes:
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");


-- 
Nov 07 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1644


schveiguy yahoo.com changed:

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





I'm changing this to critical, because Tango cannot be compiled with D2 without



-- 
Jul 10 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1644






*** Bug 2204 has been marked as a duplicate of this bug. ***


-- 
Aug 13 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1644


bugzilla digitalmars.com changed:

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





Fixed dmd 2.018


-- 
Aug 14 2008