D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7409 - Overloading with user-defined struct when passing by ref
Summary: Overloading with user-defined struct when passing by ref
Status: RESOLVED DUPLICATE of issue 4843
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-31 03:27 UTC by jens.k.mueller
Modified: 2012-01-31 07:46 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description jens.k.mueller 2012-01-31 03:27:32 UTC
Overloading does not work with user-defined structs when there are two overloaded functions receiving the struct (one by ref and one by value).

struct MyStruct {}

void fun(ref MyStruct a) {}
void fun(MyStruct a) {}

unittest
{
    MyStruct a;
    fun(a); // fails both match
}

Tested with DMD32 D Compiler v2.057 which results in the error message:
Error: function struct.fun called with argument types:
        ((MyStruct))
matches both:
        struct.fun(ref MyStruct a)
and:
        struct.fun(MyStruct a)

But fun(ref MyStruct a) is more specialized. Thus, it should win.
The overloading protocol is defined in http://dlang.org/function.html section "Function Overloading" and in TDPL Section 5.5 "Overloading". Both description (though different in wording) specify the same behavior.

It works for int. And I assume for other built-in types as well. But I haven't checked.

I believe this issue is important. Because it is often needed (as an optimization to avoid the copy/move) for e.g. operator overloading.
Comment 1 Kenji Hara 2012-01-31 07:11:23 UTC
This issue is a part of bug 5889.
Comment 2 yebblies 2012-01-31 07:46:55 UTC

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