Compiler could elide many more postblit constructor calls

TommiT tommitissari at hotmail.com
Sat Jun 29 10:57:32 PDT 2013


On Saturday, 29 June 2013 at 13:47:36 UTC, TommiT wrote:
> [..]
>
> Example:
>
> ----
> struct S
> {
>     int[] values;
>
>     this(this)
>     {
>         values = values.dup;
>     }
> }
>
> void foo(const S) { }
>
> void main()
> {
>     const S s;
>     foo(s); // No need to call postblit
> }

One important related detail:
If the compiler decides to elide the postblit of S on the call to 
foo(s), then the destructor of S (if S happened to have one) 
should not be called when the call to foo exits and the argument 
passed to foo goes out of scope. The logic behind this is that 
when we omit the postblit on the argument that's passed by value, 
it is as-if we had passed the argument by const reference (except 
that the argument is considered local to foo, i.e. not returnable 
by reference and what not).


More information about the Digitalmars-d mailing list