Issue 12180 - NRVO with multiple return statements
Summary: NRVO with multiple return statements
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-16 03:53 UTC by Simon
Modified: 2022-12-17 10:42 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simon 2014-02-16 03:53:04 UTC
In the following code, no return value optimization is performed because foo contains multiple return statements. But it would be valid to do so, i.e. bitcopy the appropriate value into the return value of foo without calling the postblit (and destructor).

In C++, such optimization would be invalid in general. But in D, the struct is guaranteed not to contain pointers to itself. Therefore structs can be moved without destructor/postblit.

struct S
{
    this(this) { writefln("copy"); }
}

S foo(bool x)
{
    S a, b;
    if(x) return a;
    else return b;
}

void main() { foo(true); }