D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4103 - opAssign signature rules not enforced on templated opAssign
Summary: opAssign signature rules not enforced on templated opAssign
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, spec
Depends on:
Blocks:
 
Reported: 2010-04-19 00:59 UTC by Lars T. Kyllingstad
Modified: 2015-02-09 14:35 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 Lars T. Kyllingstad 2010-04-19 00:59:09 UTC
The "Operator Overloading" page of the spec says that:

   "...the following parameter signatures for opAssign
    are not allowed:
    ...
    opAssign(T)
    ...
    where T is the same type as the aggregate type A..."


However, the following compiles:

    import std.stdio;

    struct S
    {
        S opAssign(T)(T t)
        {
            writeln(T.stringof);
            return this;
        }
    }

    void main()
    {
        S a, b;
        a = b;
    }

When run, it prints 'S'.
Comment 1 Lars T. Kyllingstad 2010-04-19 01:01:07 UTC
I am using DMD 2.043.
Comment 2 Kenji Hara 2015-02-09 14:35:20 UTC
D2 spec is updated for struct.

http://dlang.org/operatoroverloading#assignment

> For struct types, operator overloading for the identity assignment is allowed.
...
> However for class types, identity assignment is not allowed. 

And for class types, identity opAssign definition is properly checked, even if it's template.

class C
{
    C opAssign(T)(T t)    // line 3
    {
        writeln(T.stringof);
        return this;
    }
}

void main() {}

Output:

test.d(3): Error: class test.C identity assignment operator overload is illegal