Ranges: is it ok if front is a data member?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Dec 12 08:43:35 PST 2013


On Thu, Dec 12, 2013 at 05:19:28PM +0100, Adam D. Ruppe wrote:
> Consider the following:
> 
> struct JustZeroes {
>     int front = 0;
>     enum bool = false;
>     void popFront() {}
> }
> 
> Is that guaranteed to work as an input range? I ask because I've so
> often written:
> 
>   T current;
>   @property T front() { return current; }
> 
> that it just seems silly to me to write the extra lines when current
> == front. I realize there is a small difference there, in that front
> is not an lvalue here, but is when it is a direct member, but other
> than that, is this an acceptable form? Or does the lvalue thing mean
> it is strongly discouraged?

I do this with my own ranges sometimes. Sometimes, it's more performant
to precompute the value of .front and store it (as .front), and have
.popFront compute the next value, than to have .front compute the value
every time. AFAICT, this is perfectly fine and should work with Phobos
seamlessly. The power of ducktyping!


T

-- 
Quack!


More information about the Digitalmars-d-learn mailing list