Topic: Scope of functions default argument, (what does c++ std say)


Author: kfreeman@access4.digex.net (Keith Freeman)
Date: 28 Dec 1994 17:27:35 GMT
Raw View
Abed Hammond (abed@ritz.cec.wustl.edu) wrote:
: 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;
: }
<cut>

According to the ARM (p 268) "The exact point of destruction is
[of a temporary] implementation dependent."  In fact, the actual
decision of whether to use a temporary or not in a given
situation is also implementation dependent.  I too have seen
different compilers give different results.

Keith Freeman
Loral Aerosys, Inc.




Author: krotoff@top.rector.msu.su (Alexsander Krotoff)
Date: 28 Dec 1994 18:28:40 GMT
Raw View

In article <3ds767$m51@news1.digex.net> (28 Dec 1994 17:27:35 GMT) Keith Freeman wrote:
: According to the ARM (p 268) "The exact point of destruction is
: [of a temporary] implementation dependent."  In fact, the actual
: decision of whether to use a temporary or not in a given
: situation is also implementation dependent.  I too have seen
: different compilers give different results.

As far as I know standard commety changed this: now temporary shall
be deleted at the end of expression wich use it.
--
Alexander Krotoff    <krotoff@such.srcc.msu.su>
Research Computer Center   [Moscow]939-2638
Moscow State University    MGU, SRCC k316. GZ B-733r.




Author: abed@ritz.cec.wustl.edu (Abed Hammond)
Date: 8 Nov 1994 23:41:00 -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.