www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4651] New: Docs: Returned classes that have access to stack variables of its enclosing function

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

           Summary: Docs: Returned classes that have access to stack
                    variables of its enclosing function
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: www.digitalmars.com
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2010-08-15
19:23:25 PDT ---
This is an outdated statement:

"While a non-static nested class can access the stack variables of its
enclosing function, that access becomes invalid once the enclosing function
exits: 

class Base
{
    int foo() { return 1; }
}

Base func()
{   int m = 3;

    class Nested : Base
    {
    int foo() { return m; }
    }

    Base b = new Nested;

    assert(b.foo() == 3);    // Ok, func() is still active
    return b;
}

int test()
{
    Base b = func();
    return b.foo();        // Error, func().m is undefined
}
"

As is stated in TDPL, variables in the local function get allocated on the heap
if DMD detects that a returning class needs access to it. This section needs to
be updated.

The next section with the code example for a workaround can be removed:

"If this kind of functionality is needed, the way to make it work is to make
copies of the needed variables within the nested class's constructor"

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 15 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4651



--- Comment #1 from github-bugzilla puremagic.com 2012-01-23 21:43:00 PST ---
Commit pushed to
https://github.com/D-Programming-Language/d-programming-language.org

https://github.com/D-Programming-Language/d-programming-language.org/commit/c58bfaea9250e432ec3929bc59f0bad30f006812
fix Issue 4651 - Docs: Returned classes that have access to stack variables of
its enclosing function

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


Walter Bright <bugzilla digitalmars.com> changed:

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


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



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2012-01-23
22:30:37 PST ---
(In reply to comment #1)
 Commit pushed to
 https://github.com/D-Programming-Language/d-programming-language.org
 
 https://github.com/D-Programming-Language/d-programming-language.org/commit/c58bfaea9250e432ec3929bc59f0bad30f006812
 fix Issue 4651 - Docs: Returned classes that have access to stack variables of
 its enclosing function
Since you're on a roll (Walter) you could also $(D1) wrap the section on nested functions, Issue 4556. If not, I'll make a pull myself. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 23 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4651



--- Comment #3 from Walter Bright <bugzilla digitalmars.com> 2012-01-23
23:30:43 PST ---
Feel free to do a pull.

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