www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5690] New: Struct const member makes struct const

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

           Summary: Struct const member makes struct const
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: tarossi gmail.com



int main(string[] args) {
    auto s1 = f(); // MH MH
    auto s2 = g(); // OK
    s2.c = null; // OK
    return 0;
}

class C {}

struct StructWithConstMember {
    this(int i, C c) { this.i=i; this.c=c; }
    int i;
    const(C) c;
}

struct StructWithoutConstMember {
    this(int i, C c) { this.i=i; this.c=c; }
    int i;
    C c;
}

ref StructWithConstMember f() {
    return * new StructWithConstMember(1, new C); // ERROR
}

ref StructWithoutConstMember g() {
    return * new StructWithoutConstMember(1, new C); // OK
}


src\main.d(27): Error: *new StructWithConstMember(1,new C) is not mutable

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 03 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5690


Stewart Gordon <smjg iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |spec
                 CC|                            |smjg iname.com



It only makes sense that it should make it const - to reassign the struct would
violate the constancy of the const member.  Though the MBNR approach (see issue
2625) would also work.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5690


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |INVALID



00:06:54 PST ---
Right, this is not a compiler bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 24 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5690


Kenji Hara <k.hara.pg gmail.com> changed:

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




 Right, this is not a compiler bug.
No, this is a compiler bug.
 It only makes sense that it should make it const - to reassign the struct would
 violate the constancy of the const member.
A returned value from f() is *mutable* object, so to reassign the part of it is still valid. f().i = 10; // field i is mutable so this code should compile. This issue is a dup of bug 6366, that has more better summary "Issue 6336 - Can't return ref T where T has const/immutable members". -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 24 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5690




Notice:

A NewExpression like (new StructWithConstMember(1, new C)) makes rvalue, but
dereferencing it (* new StructWithConstMember(1, new C)) makes *lvalue*.
So returning it from function by ref is valid.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 24 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5690


Kenji Hara <k.hara.pg gmail.com> changed:

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



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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 24 2012