1

I have to build a LPoint3f with 3 arguments which are the result of dgi.get_int32() / 100.0. In Python I can rely on the calling order:

LPoint3f(dgi.get_int32() / 100.0, dgi.get_int32() / 100.0, dgi.get_int32() / 100.0);
# values are correct

When translating this code to C++ I wrote:

LPoint3f pos(dgi.get_int32() / 100.0, dgi.get_int32() / 100.0, dgi.get_int32() / 100.0);
// incorrect values sometimes, correct others

However, this works:

x = dgi.get_int32() / 100.0;
y = dgi.get_int32() / 100.0;
z = dgi.get_int32() / 100.0;
LPoint3f pos(x, y, z);

Is code #1 undefined behavior?

thor
  • 21,418
  • 31
  • 87
  • 173
Nacib Neme
  • 859
  • 1
  • 17
  • 28

1 Answers1

3

Order of function calls in C++ is unspecified. If your calls get_int32() have any side effects, i.e. modifying some internal value, then you will not get the same result as in Python. The code is not undefined but it will not produce reliable values.

2501
  • 25,460
  • 4
  • 47
  • 87