I am sorry for this...

I

Ioannis Vranos

Nakor said:
My sentiments exactly. Unfortunately I can not send you her email as I
would certainly get in trouble with the school. I was very annoyed to
have to do this assignment.


And how will she understand that you fulfilled this "assignment"?
 
J

Jakob Bieling

IMHO it is.

Ok, now please consider the following snippet:

int const& f ()
{
return 2;
}

int main ()
{
int i = f();
}

Now we do not use our const reference to initialize another reference,
but assign the value to another variable right away. So, as the temporary is
bound to the const reference return value, and that return value goes out of
scope after the ';' .. this should be valid? But is it, tho it is
contradictory to the passage Victor cited.

thanks!
 
V

Victor Bazarov

Jakob said:
[...]
Ok, now please consider the following snippet:

int const& f ()
{
return 2;
}

int main ()
{
int i = f();
}

Now we do not use our const reference to initialize another reference,
but assign the value to another variable right away. So, as the temporary is
bound to the const reference return value, and that return value goes out of
scope after the ';' .. this should be valid? But is it, tho it is
contradictory to the passage Victor cited.

Which one of the two ';' do you mean when you say that "return value goes
out of scope after the ';'"?

Of course, I'd really prefer an example involving classes. In your case
the '2' is a literal and has a life of its own. And while the Standard
does talk about a potential creation of a temporary during the binding of
a reference to an rvalue, how can we make sure there is a temporary in
this case? I can only think of one method: a constructor that is called
for the temporary. So, updating your code to

struct A { int i; A(int i) : i(i) {}; operator int() { return i; }};
A const& f()
{
return 2;
}
int main()
{
int i = f();
}

is it legal or does the temporary get destroyed at the 'f's closing brace?

Thanks.

V
 
A

Alf P. Steinbach

* Victor Bazarov:
struct A { int i; A(int i) : i(i) {}; operator int() { return i; }};
A const& f()
{
return 2;
}
int main()
{
int i = f();
}

is it legal or does the temporary get destroyed at the 'f's closing brace?

Answering my own original innocent question, now escalated into a full-blown
debate: it's the latter (as would also be the case with just using 'int').

§12.2/5 "A temporary bound to the returned value in a function return
statement (6.6.3) persists until the function exits".

If this was not the case, then gross inefficiencies would have to be
introduced to allocate memory for the result of a function for the
general case. With a reference result the referred object can be
any size whatsoever. Which means that the calling code cannot pre-
allocate space on the stack.

But of course we can wonder _why_ the standard requires near certain UB
(only if you don't use the result in any way can you avoid UB) instead
of simply disallowing this thing. Well, it may be the case that it one
return path the function returns a valid reference, while in another,
just a temporary to satisfy that warning-addicted compiler. Or it may
be the case that the programmer is engaging in Unholy Practices to obtain
the stack-pointer.

Although I would have preferred that the standard disallowed this...

Cheers,

- Alf
 
H

Howard

Nakor said:
My sentiments exactly. Unfortunately I can not send you her email as I
would certainly get in trouble with the school. I was very annoyed to
have to do this assignment.

You shouldn't be annoyed. I have found through experience that newsgroups
are an incredibly helpful tool for my programming. I've solved countless
problems by asking questions in the newsgroups, by reading answers to
others' questions, and by joining in discussions/debates. An assignment
like you've been given is meant to introduce all the students to a valuable
asset, one that every programmer should know about and know how to make good
use of. (Google is another one that's invaluable, by the way.) Spend some
time just lurking here... you'll learn a LOT!

-Howard
 
I

Ioannis Vranos

Jakob said:
Ok, now please consider the following snippet:

int const& f ()
{
return 2;
}

int main ()
{
int i = f();
}

Now we do not use our const reference to initialize another reference,
but assign the value to another variable right away. So, as the temporary is
bound to the const reference return value, and that return value goes out of
scope after the ';' .. this should be valid? But is it, tho it is
contradictory to the passage Victor cited.

thanks!


No the above is not valid (meaning it invokes undefined behaviour).
 
I

Ioannis Vranos

Victor said:
Which one of the two ';' do you mean when you say that "return value goes
out of scope after the ';'"?

Of course, I'd really prefer an example involving classes. In your case
the '2' is a literal and has a life of its own.



Still the known sure life-time of the literal is the function scope and
in most cases an int(2) gets created upon the return statement in the
function scope, a const reference is returned to this temporary which is
destroyed after the return statement, that is, return 2; in this case is
equivalent to return int(2);


So in this case both the literal's known life-time and the life-time of
the possible temporary is the function scope, and this means we get
undefined behaviour.


And while the Standard
does talk about a potential creation of a temporary during the binding of
a reference to an rvalue, how can we make sure there is a temporary in
this case? I can only think of one method: a constructor that is called
for the temporary. So, updating your code to

struct A { int i; A(int i) : i(i) {}; operator int() { return i; }};
A const& f()
{
return 2;
}
int main()
{
int i = f();
}

is it legal or does the temporary get destroyed at the 'f's closing brace?


The above is equivalent to


struct A { int i; A(int i) : i(i) {}; operator int() { return i; }};

A const& f()
{
return A(2);
}
int main()
{
int i = f();
}


which of course is the same case, since a reference to a local temporary
is returned, while in the original case a reference to a local literal
or temporary is also returned.
 
N

Nakor

Heh :) L*** A**** know's I am a lippy asshat. She know's I say what I
mean and mean what I say. I don't think she'd be too angry with me. :)
 
N

Nakor

Howard said:
An assignment
like you've been given is meant to introduce all the students to a valuable
asset, one that every programmer should know about and know how to make good
use of. (Google is another one that's invaluable, by the way.) Spend some
time just lurking here... you'll learn a LOT!

Yes, a valuable asset, but one I was already very aware of. I have a
couple of message boards that I use when I absolutely have to. For now
I love my books. When my books begin to fail me I will turn more to
the net.

Nakor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top