Search code examples
c++functionexpressioniso

C++ execution order when passing function arguments


A container class with the following interface:

template <typename T> class DynArray {
  /// Returns the number of elements in the array.
  inline size_t GetCount();
  /// Releases the internal memory from the \class DynArray
  /// and returns it. The memory must be deallocated manually.
  inline T* Release();
}

In a function call like

SomeFunction(arr.GetCount(), arr.Release())

I would have expected arr.GetCount() to be called before arr.Release(), but the reverse seems to actually happen causing the first parameter to be passed a value of 0 instead of the actual array size. I'm using Visual Studio 2012.

Does the C++ standard say anything specific about order of execution when evaluating function parameters?


Solution

  • It says that the order is entirely unspecified.

    The sequencing rules are far too complex to reproduce here, and it's hard to prove a negative, but a non-normative note conveniently summarises it for us:

    [C++11: 5.2.2/4]: When a function is called, each parameter (8.3.5) shall be initialized (8.5, 12.8, 12.1) with its corresponding argument. [ Note: Such initializations are indeterminately sequenced with respect to each other (1.9) — end note ] [..]

    (Identical text in C++14.)