[Issue 11307] New: Make const(T).init and immutable(T).init lvalues
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Oct 20 10:27:06 PDT 2013
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 at puremagic.com
ReportedBy: monarchdodra at gmail.com
--- Comment #0 from monarchdodra at 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: -------
More information about the Digitalmars-d-bugs
mailing list