D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2282 - Struct copy-constructor should call opAssign
Summary: Struct copy-constructor should call opAssign
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 enhancement
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-08-14 09:56 UTC by Koroskin Denis
Modified: 2015-06-09 05:15 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Koroskin Denis 2008-08-14 09:56:12 UTC
opAssign isn't called upon copy-construction.

I think that the following:

struct Test {
    void opAssign(Test other) {
       // ...
    }
    // ...
}

Test t1;
Test t2 = t1;

should be translated into the following:

Test t1;
Test t2 = void;
t2 = t1;
Comment 1 Don 2009-08-04 06:44:30 UTC
This behaviour is by design. I'm marking it as an enhancement, but it should probably be closed as invalid. It's definitely not a wrong-code bug.
Comment 2 Don 2009-11-23 00:30:25 UTC
Invalid: use postblit instead.
When the types are identical, this(this) gets called, not opAssign.
Comment 3 Koroskin Denis 2009-11-23 02:43:39 UTC
In D2, yes, but what about D1?
Comment 4 Don 2009-11-23 03:00:24 UTC
(In reply to comment #3)
> In D2, yes, but what about D1?

The spec is quite clear:

"The assignment operator cannot be overloaded for rvalues that can be implicitly cast to the lvalue type." (operatoroverloading.html#Assignment).

And when I compile the initial code, I get a compiler error message which is clear and which is in perfect agreement with the spec:

bug.d(19): Error: function test.Test.opAssign identity assignment operator ove
rload is illegal
Comment 5 Koroskin Denis 2009-11-23 03:15:55 UTC
Great, thanks!