Vladimir wrote:
> Victor Bazarov wrote:
>
>>>call(++i, ++i);
>>>
>>>which can result in both arguments being the same!
>>>That's not really random to me 
>>
>>The expression above has undefined behaviour because the object 'i'
>
> has
>
>>its _stored_value_ changed more than once between sequence points.
>
>
> Similar situation is with rand() - after inlining, internal value
> (used to hold random state) is changed more than once - that's why
> the problem occurs.
Similar, but no undefined behaviour, only unspecified order of calls.
Every function call is surrounded by sequence points, so even with
inlining there would be at least four of them between the program
decided to call the first 'rand' and calling the 'call' function. So,
the change to some stored value (the side effect of 'rand') does not
happen more than once between sequence points.
>>There isn't any. Use separately declared/defined/initialised
>
> objects:
>
>> int r1 = rand(), r2 = rand();
>> Vect2D v(r1, r2);
>
>
> I wish we could always remember to use this when needed.
And I wish I were young, slim, and healthy.
> What about encapsulating such functions with internal state
> into some special classes which would prevent problems
> (possibly by preventing inlining, etc.)?
You can try limiting the members of your programming team to using
some kind of class for that, or a macro, or whatever would resolve
this issue, but the language does not provide a mechanism (yet) to
catch all instances of unspecified behaviour. Of course we can always
hope for better tools at our disposal...
> This would make coding much more reliable - which is
> essential in large serious projects.
I believe you could use some kind of "PC-lint"-like code checker that
might catch that.
> P.S. These little things are really important, people.
> They usually make the difference between 99% and 100%
> bug-free software, so I think we shouldn't ignore them.
Of course we shouldn't. And _we_ won't. It's the programmers who don't
read comp.lang.c++ we should be worrying about
V