What is the order of destruction of function parameters?

前端 未结 1 1261
误落风尘
误落风尘 2020-12-17 14:24

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

相关标签:
1条回答
  • 2020-12-17 14:49

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

    0 讨论(0)
提交回复
热议问题