getNext

Shin Fujishiro rsinfu at gmail.com
Tue Jul 13 03:09:42 PDT 2010


Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> I've just had an idea that is so dark and devious, I was almost afraid 
> to try it. But it works like a charm. Consider:
> 
> T * getNext(R, E)(ref R range,
>                    ref E store = *(cast(E*) alloca(E.sizeof))
> {
>      ...
> }
> 
> With this, allocating a dummy buffer on caller's stack is automated, so 
> client code can just write:
> 
> for (T * p; (p = getNext(r)); )  {
>     ... process *p ...
> }
> 
> I feel dirty.

How about a TLS variable?

template temporary(T)
{
    static T temporary;
}
E* getNext(R, E)(ref R range, ref E store = temporary!E);


Shin


More information about the Digitalmars-d mailing list