Topic: Order of distruction of default function arguments.


Author: abed@ritz.cec.wustl.edu (Abed Hammond)
Date: 12 Nov 1994 10:34:24 -0600
Raw View
Hello,

I am interested in knowing whether temporary objects generated for functions
default arguments has function scope. I did the following
experiment using g++2.6.0 and the sgi C++ compiler NCC v1.0 and I got
different results as shown below.

-----------------------------------cut here-----------------------------
#include <iostream.h>

class A {

public:
    A() {cout << "\nA Ctor" << endl; }
    A(A&) {cout << "\nA(A)" << endl; }
    ~A() {cout << "\nA Dtor" << endl; }
    A& operator = (A& A) {cout << "\nA = " << endl; return *this; }
};

void print(A& a = A()); // A() creates a temp for default argument.

main() {
    cout << "\nIn main" << endl;

    cout << "\ncall 1" << endl;
    print(A);
    cout << "\call 1" << endl;

    cout << "\ncall 2" << endl;
    print(A);
    cout << "\call 2" << endl;

    cout << "\nOut of main" << endl;
}
void print(A& a) {
   cout << "\nprint" << endl;
}

with g++ 2.6.0 the above program prints the following:

In main
call 1
A Ctor
print
A Dtor
call 1

call 2
A Ctor
print
A Dtor
call 2

Out of main

Which implies that the temporary has function scope. With SGI NCC v1.0
I get the following:

In main

call 1
A Ctor
print
call 1

call 2
A Ctor
print
call 2
Out of main

A Dtor
A Dtor

Which implies that the temporary has file/global (?) scope. So which is
correct. Thanks for any comments.

Abed Hammoud, D.Sc.
Stealth Technologies, Inc.




Author: jason@cygnus.com (Jason Merrill)
Date: Mon, 14 Nov 1994 10:25:24 GMT
Raw View
>>>>> Abed Hammond <abed@ritz.cec.wustl.edu> writes:

> I am interested in knowing whether temporary objects generated for
> functions default arguments has function scope.