dlang.org icon indicating copy to clipboard operation
dlang.org copied to clipboard

Document NRVO is applied in simple cases

Open dlangBugzillaToGithub opened this issue 12 years ago • 2 comments

Denis Shelomovskii (@denis-sh) reported this on 2013-06-16T01:41:03Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=10372

CC List

  • Lars T. Kyllingstad (@kyllingstad)

Description

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.

dlangBugzillaToGithub avatar Jun 16 '13 01:06 dlangBugzillaToGithub

post (@kyllingstad) commented on 2013-11-05T10:31:21Z

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.

dlangBugzillaToGithub avatar Nov 05 '13 10:11 dlangBugzillaToGithub

verylonglogin.reg commented on 2013-11-05T11:49:44Z

(In reply to comment #1)
> ...
>       auto s = makeS(true);

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

dlangBugzillaToGithub avatar Nov 05 '13 11:11 dlangBugzillaToGithub