dlang.org
dlang.org copied to clipboard
Document NRVO is applied in simple cases
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.
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.
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.