Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reference to the temporary object

Reply
Thread Tools

reference to the temporary object

 
 
Raider
Guest
Posts: n/a
 
      06-22-2006
is such code correct?

#include <string>
typedef std::string T;

T *s;

const T& foo(const T& default)
{
if (s) return *s; else return default;
}

int main()
{
const T& value = foo("bar");
// use value
}

I think the "bar" must be destroyed immediatly when foo() returns.

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      06-22-2006
Raider posted:

> is such code correct?
>
> #include <string>
> typedef std::string T;
>
> T *s;
>
> const T& foo(const T& default)
> {
> if (s) return *s; else return default;
> }



"default" is a keyword in C++.


const T &foo(const T &def)
{
if(s) return *s;

return def;
}


> int main()
> {
> const T& value = foo("bar");
> // use value
> }
>
> I think the "bar" must be destroyed immediatly when foo() returns.


Step 1: A non-const, nameless, R-value temporary is created.
Step 2: It is passed by const reference to a function.
Step 3: The function then returns it by const reference.
Step 4: The calling function binds a reference named "value" to the
returned reference.

If you bind a reference to a temporary, the temporary's lifetime is
extended to that of the reference. However, there's only one Step in
which this happens: Step 1. The argument to the function gets destroyed
once the function returns. The function returns an invalid reference.
You're left with a reference to an invalid object.

 
Reply With Quote
 
 
 
 
Raider
Guest
Posts: n/a
 
      06-22-2006
Frederick, thanks for support. I get it.

> If you bind a reference to a temporary, the temporary's lifetime is
> extended to that of the reference. However, there's only one Step in
> which this happens: Step 1. The argument to the function gets destroyed
> once the function returns. The function returns an invalid reference.
> You're left with a reference to an invalid object.


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      06-22-2006
Raider posted:

> Frederick, thanks for support. I get it.
>
>> If you bind a reference to a temporary, the temporary's lifetime is
>> extended to that of the reference. However, there's only one Step in
>> which this happens: Step 1. The argument to the function gets

destroyed
>> once the function returns. The function returns an invalid reference.
>> You're left with a reference to an invalid object.



(Some gentle advice: If you top-post here, a lot of people will ignore
you.)

It's nice to try write code to prove things... I do it all the time! Take
this for example:

(Unchecked code)

class ArbitraryClass
{
public:

bool is_valid;

ArbitraryClass() : is_valid(true) {}

~ArbitraryClass() { is_valid = false; }
};


#include <cstdlib>


const ArbitaryClass &Func( const ArbitraryClass &arg )
{
static ArbitraryClass obj;

if ( std::rand() == 52 ) return obj;

return arg;
}

#include <iostream>

int main()
{
const Arbitrary &ref = Func( ArbitraryClass() );

std::cout << "Is the object referred to by ref valid? "
<< ( ref.is_valid ? "true" : "false" );

}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Passing a temporary object to a reference parameter Martin Rennix C++ 3 05-07-2007 04:33 AM
Const reference to a temporary object - Why? Dave Rahardja C++ 15 11-03-2005 03:45 AM
Type conversion of reference to a temporary object Stefan Strasser C++ 2 03-28-2005 09:52 PM
A reference to non-const to be bound to a temporary object John Ky C++ 9 02-23-2004 12:53 AM



Advertisments