[Issue 2070] New: DMD should allow easy creation of stack-allocated classes
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon May 5 11:16:44 PDT 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2070
Summary: DMD should allow easy creation of stack-allocated
classes
Product: D
Version: 1.029
Platform: PC
OS/Version: Windows
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: schveiguy at yahoo.com
Currently, if I want to temporarily create a stack-allocated class, then pass
it to a function, then delete it after the function is done, I have to do this:
int f(C c) {...}
int i;
{
scope c = new C();
i = f(c);
} // c is destroyed
It would be nice if the above code could be one line. Perhaps something like:
int i = f(scope new C());
This is really nice for classes that are stream or filter-type classes.
Generally one only creates the filter-type class to be used temporarily for a
single function call. For instance, in Tango, one can iterate through the
lines of a file by doing:
foreach(line; new LineInput(new FileInput("file.txt")))
{
// do something with line.
}
Both the anonymous LineInput and FileInput classes can be stack-allocated,
saving a call to the heap. This example is probably not quintessential, as
FileInput allocates heap space for buffering, but LineInput AFAIK does not.
Other examples can be thought of that would require no heap allocations,
thereby saving the performance hit of allocating heap data.
--
More information about the Digitalmars-d-bugs
mailing list