Reputation: 17910
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?
Upvotes: 17
Views: 650
Reputation: 60999
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#
.
Upvotes: 14