Compiler could elide many more postblit constructor calls

Diggory diggsey at googlemail.com
Sat Jun 29 19:20:11 PDT 2013


On Saturday, 29 June 2013 at 17:57:33 UTC, TommiT wrote:
> 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).

Unless the function is pure, this is only possible for immutable 
parameters otherwise the original variable may be modified while 
inside the function even if it is passed by const.


More information about the Digitalmars-d mailing list