www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.bugs - [Issue 2070] New: DMD should allow easy creation of stack-allocated classes

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 digitalmars.com
        ReportedBy: schveiguy 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.


-- 
May 05 2008