This is a follow-up to my previous question What is the order of destruction of function arguments? because I accidentally confused arguments with parameters. Thanks to Columbo and T.C. for clearing me of terminology confusion in the comments of that question.
If the body of some function f
with parameters p_1
, ..., p_n
of types T_1
, ..., T_n
respectively throws an exception, finishes or returns, in what order are the parameters destroyed and why? Please provide a reference to the standard, if possible.
Examples:
template <typename ... Args>
void f(Args ... params) {} // in what order are params destroyed?
void f(T1 p1, T2 p2, T3 p3) {} // in what order are p1, p2 and p3 destroyed?
terminology - Name of Property: The Order Of Parameters in a Function Does Not Matter - Mathematics Stack Exchange.
Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.
When the function is called, none, one, some, or all of the default arguments can be provided and order does not matter. Default arguments can be combined with non-default arguments in the function's call.
The C++ standard does not specify the order in which function parameters need to be evaluated. The calling convention dictates the order in which the parameters are passed, but there's no requirement to evaluate them the same way.
The exact point in time at which parameters are destroyed is unspecified:
CWG decided to make it unspecified whether parameter objects are destroyed immediately following the call or at the end of the full-expression to which the call belongs.
The order in which parameters are constructed is unspecified as well, but because function parameters have block scope, although their order of construction is unspecified, destruction is in the reverse order of construction. E.g. consider
#include <iostream>
struct A {
int i;
A(int i) : i(i) {std::cout << i;}
~A() {std::cout << '~' << i;}
};
void f(A, A) {}
int main() {
(f(0, 1), std::cout << "#");
}
prints 10#~0~1
with GCC and 01#~1~0
with Clang; they construct parameters in different orders, but both destroy in the reverse order of construction, at the end of the full-expression the call occurs in (rather than right after returning to the caller). VC++ prints 10~0~1#
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With