Issue 10372 - Document NRVO is applied in simple cases
Summary: Document NRVO is applied in simple cases
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: spec
Depends on: 10371
Blocks:
  Show dependency treegraph
 
Reported: 2013-06-16 01:41 UTC by Denis Shelomovskii
Modified: 2022-12-17 10:45 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Denis Shelomovskii 2013-06-16 01:41:03 UTC
If documentation will guarantee NRVO is applied in simple cases (i.e. no copy construction occurs) structs with disabled default construction will be able to be returned from functions (currently it is not guaranteed to compile):
---
struct S
{ @disable this(this); }

S makeS()
{
    S s = S();
    return s;
}

void main()
{
    S s = makeS();
}
---

Also Issue 10371 have to be fixed first.
Comment 1 Lars T. Kyllingstad 2013-11-05 10:31:21 UTC
Even for cases where NRVO can't be applied, the spec should guarantee that the returned struct is moved and not copied.  As far as I can tell, move-on-return can be applied whenever a struct is created on the stack and then returned.

Here's an example of a case where NRVO can't necessarily be applied, but which should still compile:

  struct S { @disable this(this); }

  S makeS(bool b)
  {
      S s1;
      S s2;
      return b ? s1 : s2;
  }

  void main()
  {
      auto s = makeS(true);
  }

Note that this compiles today, it just needs to be documented in the spec.
Comment 2 Denis Shelomovskii 2013-11-05 11:49:44 UTC
(In reply to comment #1)
> ...
>       auto s = makeS(true);

Only `makeS` is related to this issue. This code is for Issue 10371.