parameter pack to inputRange

Dicebot via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 9 03:44:10 PDT 2016


On Sunday, 8 May 2016 at 23:48:01 UTC, Erik Smith wrote:
> Thanks! The static array version works for me too.   It would 
> be good to understand more about what is going on.  It looks 
> like the cost of the static array is an extra copy for each 
> element.  Maybe there is still a way to avoid that.

The difference is quite simple: my latest version does save copy 
of arguments in static array which means you can freely pass it 
around with referenced data being valid. But it may become 
expensive to copy with large argument lists if compiler can't 
optimize it away. Ali version generates a compile-time switch for 
index that returns either literals or external variables by 
referencing their symbol directly. If this gets used to reference 
external stack variables which later get out of scope, range may 
either start iterating over garbage memory or silently allocate a 
closure (don't remember which is the case for latest compiler).

In practice, if you use good inlining compiler, like LDC, I'd say 
static array version is strictly better. It is guaranteed to be 
safe and RVO allows compiler to completely get rid of additional 
stack copies if usage scope is limited.


More information about the Digitalmars-d-learn mailing list