[Issue 7353] New: NRVO not properly working with inferred return type

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jan 23 07:27:38 PST 2012


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

           Summary: NRVO not properly working with inferred return type
           Product: D
           Version: D1 & D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: performance
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: mrmocool at gmx.de


--- Comment #0 from Trass3r <mrmocool at gmx.de> 2012-01-23 16:27:36 CET ---
import std.stdio;
struct S
{
    static uint ci = 0;
    uint i;

    this(int x)
    {
        i = ci++;
        writeln("new: ", i);
    }

    this(this)
    {
        i = ci++;
        writeln("copy ", i);
    }

    ~this()
    {
        writeln("del ", i);
    }

    S save1() // produces 2 copies in total
    {
        S s = this;
        return s;
    }

    auto save2() // produces 3 copies in total
    {
        S s = this;
        return s;
        pragma(msg, typeof(return));
    }

    S save3()
    {
        return this;
    }
}

void main()
{
    {
    S s = S(1);
    S t = S(1);

    t = s.save1();
    }

    writeln("-");
    S.ci = 0;
    {
    S s = S(1);
    S t = S(1);
    t = s.save2();
    }

    writeln("-");
    S.ci = 0;
    {
    S s = S(1);
    S t = S(1);
    t = s.save3();
    }
}


$ dmd -run test.d
//or dmd -release -run test.d
//or dmd -release -O -run test.d
S
new: 0
new: 1
copy 2
del 1
del 2
del 0
-
new: 0
new: 1
copy 2
copy 3
del 2
del 1
del 3
del 0
-
new: 0
new: 1
copy 2
del 1
del 2
del 0

$ dmd -release -O -inline -run test.d
S
new: 0
new: 1
copy 2
del 1
del 2
del 0
-
new: 0
new: 1
copy 2
copy 3
del 2
del 1
del 3
del 0
-
new: 0
new: 1
del 1
del 0
del 0

-- 
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