On Thursday, 25 August 2016 at 14:42:28 UTC, Basile B. wrote:

I'll add

* create temporaries based on the const function attribute.

Struct method constness (as in your example) does not mean that the return value is constant when calling it twice in a row. As pointed out by others, the function needs to be pure. Dlang pureness is not a strong enough guarantee.
For example, this is explicitly allowed by the spec:
```
pure int foo()
{
debug writeln("in foo()"); // ok, impure code allowed in debug statement
    return 1;
}
```
That makes it illegal to transform `foo()+foo()` to `a=foo(); a+a`, at least in debug builds.

David discusses your proposed optimization, and why it cannot be done in general (!) on Dlang pure functions.
http://klickverbot.at/blog/2012/05/purity-in-d/

-Johan

Reply via email to