Formatted read consumes input

kenji hara k.hara.pg at gmail.com
Sat Sep 8 05:10:47 PDT 2012


2012/9/8 monarch_dodra <monarchdodra at gmail.com>:
[snip]
>
> Still, I find it horrible to have to create a named "dummy" variable just
> when I simply want to pass a copy of my range.

Why you are afraid to declaring "dummy" variable?
formattedRead is a parser, not an algorithm (as I said in the pull
request comment). After calling it, zero or more elements will remain.
And, in almost cases, the remains will be used other purpose, or just
checked that is empty.

int n = formattedRead(input_range, fmt, args...);
next_parsing(input_range);   // reusing input_range
assert(input_range.empty);  // or just checked that is empty

If formattedRead can receive rvalue, calling it would ignore the
remains, and it will cause hidden bug.

int n = formattedRead(r.save, fmt, args...);
// If the remains is not empty, it is ignored. Is this expected, or
something logical bug?

auto dummy = r.save;
int n = formattedRead(dummy, fmt, args...);
assert(dummy.empty);   // You can assert that remains should be empty.

formattedRead returns multiple states (the values which are read, how
many values are read, and remains of input), so allowing to ignore
them would introduce bad usage and possibilities of bugs.

Kenji Hara


More information about the Digitalmars-d mailing list