unexpected input stream behavior

Stephen M. Webb stephen.webb@bregmasoft.com
Fri Jan 11 09:37:00 GMT 2002


On January 11, 2002 11:22 am, Maarten Ditzel wrote:
> ...
> So what obviously happens is that in the first and second input
> statements, the previous key value is used in M[key]. This does not
> happen using the 3rd input statements (a and b).
> I expected the first and second input statements to act the same as
> the third, basically
>
> cin >> a >> b;
> equals
> cin >> a; cin >> b;
>
> I don't know if this is unspecified C++ behavior, or possibly a bug
> in the standard libraries or compiler.
> Any ideas/comments ?

This is a common problem with chained extraction operations.  The 
problem is that the order of evaluation of function arguments is 
explicitly undefined by the language.  The expression

  cin >> a >> b(a);

could also conceivably be written as

  operator>>(operator>>(cin, a), b(a));

it's undefined whether operator>>(cin, a) or b(a) gets evaluated first.
If either expression modifies its argument 'a', you may (or may not) 
get unexpected results.  Someone else might use the term "sequence 
points" here.

This has come up often enough that perhaps it should be a C++ or at 
least a libstdc++-v3 FAQ.  It's not a bug in the library.

__
Stephen M. Webb



More information about the Libstdc++ mailing list