www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11307] New: Make const(T).init and immutable(T).init lvalues

http://d.puremagic.com/issues/show_bug.cgi?id=11307

           Summary: Make const(T).init and immutable(T).init lvalues
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: monarchdodra gmail.com


--- Comment #0 from monarchdodra gmail.com 2013-10-20 10:27:04 PDT ---
".init" is a global property, that is marked as rvalue, to prevent it being
modified. This is correct behavior.

However, when "T" is const/immutable already, then it is illegal to modify it
anyways. Making ".init" an lvalue for such cases has advantages.

First, when writing "foo(const(T).init)", or "tmp = const(T).init", then pass
by const reference will be preferred over pass by value.

Second, a "common" use case, to memcpy initialize. This usually looks something
like:
T tmp = void;
immutable(T) init = immutable(T).init;
memcpy(&tmp, &init, T.sizeof);

Making a local copy (on the stack, or in static space) is gratuitous, and can
be avoided entirelly. This should work:
T tmp = void;
memcpy(&tmp, &immutable(T).init, T.sizeof);

This code actually happens relatively often, in array and appender.

So that's the ER. Nothing huge. Just keeping the T.init lvalue if T is const
(or immutable). There's no reason to make it rvalue.

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