[Issue 5601] A template forward reference error

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 22 03:31:57 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=5601



--- Comment #5 from Kenji Hara <k.hara.pg at gmail.com> 2013-11-22 03:31:55 PST ---
(In reply to comment #4)
> (In reply to comment #3)
> 
> > Before function body semantic finished, getting type
> > of auto function will cause forward reference error since its return type is
> > not determined.
> 
> Can't the type inferencer become a little smarter and see that the return type
> of foo is something defined in terms of itself, but it's also an int, and so
> decide it's an int?

No. In statement scope, all of statements are ordered from top to the bottom.
We can regard as the return type of auto function is declared at the end of the
function body. So seeing return type inside auto-function is exactly same as
forward reference issue.

Consider the following invalid code. If the return type is visible inside
function, compiler cannot determine the return type of foo.

auto foo(int n)
{
    if (n == 1)
        return 1;

    // forward reference to return type
    static if (is(ReturnType!foo == int))
        return "";

    assert(0);
}

Therefore it is necessary limitation to avoid indeterminate case.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list