www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10272] New: opAssign() not invoked during variable declaration and initialization

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10272

           Summary: opAssign() not invoked during variable declaration and
                    initialization
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: pwil3058 bigpond.net.au


--- Comment #0 from Peter Williams <pwil3058 bigpond.net.au> 2013-06-04
23:50:09 PDT ---
Created an attachment (id=1219)
Demonstrate the opAssign() problem for struct template

The opAssign() operator is not called when the assignment is part of a variable
declaration for both the case where auto used and where the type is declared
explicitly. e.g.:

TestStruct t1 = TestStruct(arg);
TestStruct t2 = t1;

the opAssign() operator is not called in either of these cases.  But:

TestStruct t3;
t3 = TestStruct(arg);
t3 = t1;

the opAssign() operator is called in both cases.

This can have serious consequences if (for instance) the purpose of the
opAssign() method being defined was to ensured that the assignee and the
assignor did not share the same internal array.

The attached code demonstrates the problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 04 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10272


w0rp <devw0rp gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |devw0rp gmail.com


--- Comment #1 from w0rp <devw0rp gmail.com> 2013-06-05 00:03:13 PDT ---
Isn't this exactly the same as not calling operator= in C++? I expect this
behaviour, because assignment and initialisation are two separate concepts. If
you have a case to handle where initialisation copies or destroys another
object's memory, do it in this().

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10272


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |jmdavisProg gmx.com
         Resolution|                            |INVALID


--- Comment #2 from Jonathan M Davis <jmdavisProg gmx.com> 2013-06-05 00:08:35
PDT ---
This is completely expected. Variable declarations do not use the assignment
operator, and

int a = 1;

does not use the assignment operator at all. It's only assignments which use
the assignment operator. e.g.

a = 5;

When the variable is declared, it is initialized, not assigned to. While they
may seem similar and have similar syntax, they are fundamentally different. And
as w0rp points out, this behavior is exactly the same as C++'s behavior.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10272


Peter Williams <pwil3058 bigpond.net.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |


--- Comment #3 from Peter Williams <pwil3058 bigpond.net.au> 2013-06-05
16:14:39 PDT ---
I should have been clearer.  I agree that it is the expected and desirable
behaviour when the initializer is an rValue but disagree when it is an lValue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 05 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10272


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |INVALID


--- Comment #4 from Jonathan M Davis <jmdavisProg gmx.com> 2013-06-05 17:13:09
PDT ---
Sorry, but it's still invalid.

auto foo = bar;

will _never_ use opAssign. opAssign is for overloading the assignment operator,
and there is no assignment operator in that statement. It's a variable
declaration, not an assignment. Whether it's an lvalue or rvalue is irrelevant
for that. The difference is that with an rvalue, it's a move operation, whereas
with an lvalue, the postblit operator will be called. So, if you want to
overload the behavior of

auto foo = bar;

then you need to declare a postblit constructor:

http://dlang.org/struct.html#StructPostblit

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