【问题标题】:C++ execution order when passing function arguments [duplicate]传递函数参数时的C++执行顺序[重复]
【发布时间】:2015-05-20 10:59:07
【问题描述】:

具有以下接口的容器类:

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();
}

在函数调用中

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

我原以为arr.GetCount() 会在arr.Release() 之前被调用,但实际上似乎发生了相反的情况,导致第一个参数被传递了0 的值而不是实际的数组大小。我正在使用 Visual Studio 2012。

在评估函数参数时,C++ 标准是否对执行顺序有任何具体规定?

【问题讨论】:

标签: c++ function expression iso


【解决方案1】:

它表示订单完全未指定。

排序规则过于复杂,无法在此处重现,很难证明是否定的,但非规范性注释方便地为我们总结了它:

[C++11: 5.2.2/4]: 调用函数时,每个参数 (8.3.5) 都应使用其对应的参数初始化 (8.5, 12.8, 12.1)。 [注:这样的初始化彼此之间的顺序是不确定的(1.9)—结束注][..]

(C++14 中的相同文本。)

【讨论】:

    猜你喜欢
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    相关资源
    最近更新 更多