I would like to clarify my understanding of evaluation order in Fortran.
Let's say I have a Stack type with methods pop
and push_back
.
If I execute the following code:
call stack%push_back(1)
call stack%push_back(3)
call stack%push_back(stack%pop() - stack%pop())
write(*, *) stack%pop() ! could print 2, or -2
the last element on the Stack depends on the evaluation order and as this answer explains, the Compiler is free to change the evaluation order.
But even if I have a commutative operation, there are still problems. The Fortran 2008 standard says (7.1.4):
The evaluation of a function reference shall neither affect nor be affected by the evaluation of any other entity within the statement.
So even this code:
call stack%push_back(1)
call stack%push_back(3)
call stack%push_back(stack%pop() + stack%pop())
is not standard conforming?
This means that I always have to write it like this:
call stack%push_back(1)
call stack%push_back(3)
block
integer :: A, B
A = stack%pop()
B = stack%pop()
call stack%push_back(A - B)
end block
write(*, *) stack%pop() ! is guaranteed to print 2
Is this true?
question from:https://stackoverflow.com/questions/65869750/function-evaluation-conflicting-with-other-effects-in-statement