Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of destruction of function parameters?

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?
like image 541
jotik Avatar asked May 02 '16 22:05

jotik


People also ask

Does the order of parameters in a function matter?

terminology - Name of Property: The Order Of Parameters in a Function Does Not Matter - Mathematics Stack Exchange.

What are the parameters of the function?

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.

What is the order of arguments when calling a function?

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.

Does order of parameters matter C++?

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.


1 Answers

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#.

like image 66
Columbo Avatar answered Oct 11 '22 12:10

Columbo