By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

reference to the temporary object

P: n/a
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.

Jun 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Jun 22 '06 #2

P: n/a
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.


Jun 22 '06 #3

P: n/a
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" );

}

Jun 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.