Suppose I have a function foo(x, y, z)
that is invariant w.r.t. all permutations of its arguments. I also have an iterator it
, such that the iterators it
, it + 1
and it + 2
can be dereferenced.
Is it fine to write
... = foo(*it++, *it++, *it++); // (1)
instead of
... = foo(*it, *(it + 1), *(it + 2)); // (2)
?
As far as I understand, technically it is correct since C++17 due to (quoting cppreference.com and taking into account that it
can be a raw pointer)
15) In a function call, value computations and side effects of the initialization of every parameter are indeterminately sequenced with respect to value computations and side effects of any other parameter.
The order of evaluation of function arguments is not defined, but for foo()
the order doesn't matter.
But is it an acceptable coding style? On the one hand, (1)
is nicely symmetric and implies that foo
has such an invariance, and (2)
looks somewhat ugly. On the other hand, (1)
immediately raises questions about its correctness - the one who reads the code should check the description or definition of foo
to verify the correctness of the call.
If the body of foo()
is small and the invariance is obvious from the function definition, would you accept (1)
?
(Probably, this question is opinion-based. But I can't help but ask it.)