Topic: temporary as parameter


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/15
Raw View
In article <slrn7tt326.hga.sbnaran@localhost.localdomain>, Siemel B.
Naran <sbnaran@uiuc.edu> writes
>The compiler must evaluate f1() and f2() in any order, then print the
>results in a fixed order, then destroy the temporaries in the reverse
>order.  But with careful analysis, a compiler can go this route:
>evaluate f1() print destroy, evaluate f2() in the same space print
>destroy.

only if it can see the definitions of f1() and f2() to ensure that there
are no global variables that both use and at least one alters:)


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Krzysztof Stachlewski" <kstc@box43.gnet.pl>
Date: 1999/09/14
Raw View
Look at the following code:

#include <iostream>
using namespace std;

struct A{
    char x[2];
    operator char *()
    {
        return x;
    }
};

A f()
{
    A obj;
    obj.x[0]='E';
    obj.x[1]=0;
    return obj;
}

int main()
{
 cout <<  f() << endl;
 return 0;
}

************

Is that code valid?

Function f, called in main, returns an object of type A by value.
So the object it returns, is temporary one.
Then the "A::operator char *" is called on this object,
and the result is passed to ostream::operator<<.
Can I be sure, that the temporary object will remain undestructed
until the operator<< returns? (It would be useful.)
Or maybe the compiler will destroy it, *before* calling operator<< (because
it has been already used)?
What standard says about such situation?

MSVC++ keeps the temporary BTW.

Krzysztof Stachlewski
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/14
Raw View
In article <aCqD3.81$x3.1564@news.tpnet.pl>, Krzysztof Stachlewski
<kstc@box43.gnet.pl> writes
>Is that code valid?
>
>Function f, called in main, returns an object of type A by value.
>So the object it returns, is temporary one.
>Then the "A::operator char *" is called on this object,
>and the result is passed to ostream::operator<<.
>Can I be sure, that the temporary object will remain undestructed
>until the operator<< returns? (It would be useful.)
>Or maybe the compiler will destroy it, *before* calling operator<< (because
>it has been already used)?
>What standard says about such situation?

temporaries are destroyed neither earlier nor later than the end of the
full expression in which they are created.  There is a special
requirement for temporaries bound to const references.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Greg Brewer" <nospam.greg@brewer.net>
Date: 1999/09/14
Raw View
Krzysztof Stachlewski <kstc@box43.gnet.pl> wrote in message
news:aCqD3.81$x3.1564@news.tpnet.pl...
> Look at the following code:
   [snip]
> Can I be sure, that the temporary object will remain undestructed
> until the operator<< returns? (It would be useful.)
> Or maybe the compiler will destroy it, *before* calling operator<<
(because
> it has been already used)?
> What standard says about such situation?

The standard requires that temporaries exist until statement is completed.
IOW, moved past the semicolon to the next statement.  So, you are safe.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Stephan Keil D gloekler <Stephan.Keil@gmx.de>
Date: 1999/09/14
Raw View
Krzysztof Stachlewski wrote:
>
> Look at the following code:
>
> #include <iostream>
> using namespace std;
>
> struct A{
>     char x[2];
>     operator char *()
>     {
>         return x;
>     }
> };
>
> A f()
> {
>     A obj;
>     obj.x[0]='E';
>     obj.x[1]=0;
>     return obj;
> }
>
> int main()
> {
>  cout <<  f() << endl;
>  return 0;
> }
>
> ************
>
> Is that code valid?

Yes, in main() a temporary A-object is created by copy-construction
with the temporary-A returned by f(). The temporary in main() is passed
to operator<<() and destructed afterwards.

So:

 struct A{
   A(){ cout << "A()" << endl;}
   A(const A&){ cout << "A(const A&)" << endl;}
   ~A(){ cout << "~A()" << endl;}
 };

 ostream&  operator<<(ostream& os, const A& a)
 {  return cout << "op<<()" << endl; }

 A f()
 {
     A obj;
     return obj;
 }

 int main()
 {
  cout <<  f() << endl;
  return 0;
 }

prints (of course, comments added by hand)

 A()           // f()-temp. is constructed
 A(const A&)   // copy-ctor constructs main()-temp. f()-temp
 ~A()          // f()-temp is destructed
 op<<()        // op<<() called with main()-temp.
               // endl
 ~A()          // main()-temp is destructed

> MSVC++ keeps the temporary BTW.

Good job, MSVC ;-)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/14
Raw View
On 14 Sep 99 15:56:40 GMT, Krzysztof Stachlewski <kstc@box43.gnet.pl> wrote:

>struct A{
>    char x[2];
>    operator char *()

>A f()

> cout <<  f() << endl;

>MSVC++ keeps the temporary BTW.

MSVC is right.
Unnamed temporaries are destroyed at the semi-colon -- ie, at the end
of the full expression.

--
--------------
siemel b naran
--------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/14
Raw View
On 14 Sep 1999 17:00:42 GMT, Francis Glassborow

>temporaries are destroyed neither earlier nor later than the end of the
>full expression in which they are created.  There is a special
>requirement for temporaries bound to const references.

Yes, I said basically the same thing.  However, I must add one thing.
If the compiler can show that destroying the temporary earlier has no
effect on the behaviour of the program, then it can destroy the
temporary at the earlier time and reuse the space.  This
optimization would be possible in the following expression:

   std::cout << f1() << f2() << '\n';

The compiler must evaluate f1() and f2() in any order, then print the
results in a fixed order, then destroy the temporaries in the reverse
order.  But with careful analysis, a compiler can go this route:
evaluate f1() print destroy, evaluate f2() in the same space print
destroy.


--
--------------
siemel b naran
--------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Natalie <ron@sensor.com>
Date: 1999/09/14
Raw View
Krzysztof Stachlewski wrote:
>
> Is that code valid?
>
> Function f, called in main, returns an object of type A by value.
> So the object it returns, is temporary one.
> Then the "A::operator char *" is called on this object,
> and the result is passed to ostream::operator<<.

Yes, the temporary in this case exists until the end of the
full expression, which in general is the rule.   The exeption
is when it's used as an initializer, in which case the compiler
can destroy it after the object is created.  The other is when
it's bound to a const reference, in which case it lives as long
as the reference does.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]