-preview=in might break code

Iain Buclaw ibuclaw at gdcproject.org
Sun Oct 4 19:26:56 UTC 2020


On Sunday, 4 October 2020 at 16:10:42 UTC, Ola Fosheim Grøstad 
wrote:
> On Sunday, 4 October 2020 at 14:58:13 UTC, Iain Buclaw wrote:
>> As it is an optimization, I think that's best left to the 
>> interpretation of the compiler/compiler author, however 
>> optimizations should never break the semantic guarantee.
>
> What is the intended semantic guarantee in simple words that 
> makes sense to an end user?
>
> From what you say I assume it is something along the lines of:
>
> «Has const scope semantics. Values may be passed as references. 
> Parameter passing will not trigger sideeffects on the actual or 
> formal parameters.»
>
> However, do you also have this constraint:
>
> «The caller is responsible for ensuring that the actual 
> parameter does not alias with other parameters or globals 
> available to the called function.»
>
> And if not, what do you have?
>

The spec only explicitly states that non-POD types must *always 
be passed by reference*.  So the only semantic guarantee is that 
copy constructors are elided.

Derived and user-defined data types (excluding structs and static 
arrays) are explicitly *always passed by value*.  They might 
still be passed in memory by ABI, but in that case, it results in 
a copy-to-temp.

As for everything else, unless there is a language requirement to 
pass by reference then the conservative approach would be to say 
"why bother?".  I don't immediately see a reason to do copy 
elision for trivial types unless it is provable that there'd be 
no difference in observable program behaviour.


More information about the Digitalmars-d mailing list