D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4446 - Templated function result can't be ref if tiargs are infered w/ value tiarg
Summary: Templated function result can't be ref if tiargs are infered w/ value tiarg
Status: RESOLVED DUPLICATE of issue 3175
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2010-07-11 03:02 UTC by Shin Fujishiro
Modified: 2014-02-15 02:43 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Shin Fujishiro 2010-07-11 03:02:42 UTC
Templated function (free nor member) cannot return by-ref explicitly (i.e. not with auto ref) if instantiation arguments are infered and the instantiation arguments contain at least one value argument.


This does not compile:
--------------------
void main()
{
    S s;
    s.opDispatch!"bar"(42); // fails
}
struct S
{
    ref int opDispatch(string str, T)(T arg)
    {
        return _xyz;
    }
    int _xyz;
}
--------------------
% dmd -o- -c test
test.d(8): Error: variable test.S.str only parameters or foreach declarations can be ref
--------------------


The error does not occur if the template instantiation arguments are not infered:
--------------------
void main()
{
    S s;
    s.opDispatch!("bar", int)(42); // fine
}
...
--------------------


Auto ref is fine:
--------------------
void main()
{
    S s;
    s.opDispatch!"bar"(42); // fine
}
struct S
{
    auto ref opDispatch(string str, T)(T arg)
    {
        return _xyz;
    }
    int _xyz;
}
--------------------


This is a workaround for opDispatch:
--------------------
struct S
{
    template opDispatch(string str)
    {
        ref int opDispatch(T)(T arg)
        {
            return _xyz;
        }
    }
    int _xyz;
}
--------------------
Comment 1 Shin Fujishiro 2010-09-20 22:03:05 UTC

*** This issue has been marked as a duplicate of issue 3175 ***