[Issue 16467] templated function default argument take into account when not needed

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Sep 6 04:58:36 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16467

Jonathan M Davis <issues.dlang at jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang at jmdavisProg.co
                   |                            |m

--- Comment #1 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
I suspect that this will be declared to be "not a bug." While, I can understand
your reasoning, the problem is that when you pass a string to your identity
function, IFTI instantiates it with string. It's only called _after_ the
function has been compiled, and the default argument does not work with the
type in question.

Remember that templatizing a function directly is just shorthand for declaring
an eponymous template that it's a function. So, this would be equivalent to
what you declared:

template identity(T)
{
    T identity(T t = 0)
    {
        return t;
    }
}

And if the template is instantiated with string, then the default argument is
not valid. Also consider the case where you do

auto result = identity!string();

It's exactly the same template instantiation as identity("hello"), but it would
need the default argument, which is the wrong type.

--


More information about the Digitalmars-d-bugs mailing list