Do pure functions solve the "return const" problems?

Janice Caron caron800 at googlemail.com
Tue Apr 29 09:27:52 PDT 2008


On 29/04/2008, Christopher Wright <dhasenan at gmail.com> wrote:
>  You can't just record, for a given function, what the precedence of the out
> parameters is:
>
>  void func(out int a, out int b, bool c) {
>         if (c) {
>                 a = 5;
>                 b = 9;
>         } else {
>                 b = 12;
>                 a = 3;
>         }
>  }

The compiler is fully entitled to reorder

    a = 5;
    b = 9;

into

    b = 9;
    a = 5;

if it so chooses. On a multi-core architecture, these assignments
might even happen simultaneously.


> It'd take more effort to prevent out parameters for pure functions.

That's clearly not true. Rule one is that a pure function "has
parameters that are all invariant or are implicitly convertible to
invariant". (See
http://www.digitalmars.com/d/2.0/function.html#pure-functions ). Out
parameters fail immediately on account of not being invariant.



More information about the Digitalmars-d mailing list