J
jl_post
Hi,
Recently I was wondering something:
If a function is returned from (returning a value), does the value
get returned as-is before any destructors are called? For example, if
I have a function like:
int f()
{
int i = 7;
Blah blah(i); // modifies i in destructor
return i;
}
does 7 get returned, or does i's modified value get returned?
In case a complete program is needed for clarification, here's one:
#include <iostream>
class EndOfScopeDoubler // modifies the integer in its destructor
{
public:
EndOfScopeDoubler(int & _value) : value(_value) { }
~EndOfScopeDoubler() { value *= 2; }
private:
int & value;
};
int i = 7; // global variable
int f()
{
EndOfScopeDoubler blah(i);
return i;
}
int main(int argc, char ** argv)
{
std::cout << "i (before) = " << i << std::endl;
std::cout << "f() returns " << f() << std::endl;
std::cout << "i (after) = " << i << std::endl;
return 0;
}
i (before) should be 7. i (after) should be 14. But what should f
() return? It could return 7 or 14, depending on whether the
EndOfScopeDoubler destructor fires off before the return statement or
not.
When I run this program, I see:
i (before) = 7
f() returns 7
i (after) = 14
The fact that i ended up as 14 tells me that the EndOfScopeDoubler
destructor did fire off, but the fact that f() returned 7 tells me
that the value 7 got returned before the destructor got a chance to
modify it.
(Just so you know, if I surround the line "EndOfScopeDoubler blah
(i);" with curly braces, f() will return 14.)
My question is: Is having the return value returned before the
destructor(s) fire off defined behavior? Sure, my compiler happens to
think so, but I'm wondering if this is valid C++ behavior (or simply
undefined).
Thanks.
-- Jean-Luc
Recently I was wondering something:
If a function is returned from (returning a value), does the value
get returned as-is before any destructors are called? For example, if
I have a function like:
int f()
{
int i = 7;
Blah blah(i); // modifies i in destructor
return i;
}
does 7 get returned, or does i's modified value get returned?
In case a complete program is needed for clarification, here's one:
#include <iostream>
class EndOfScopeDoubler // modifies the integer in its destructor
{
public:
EndOfScopeDoubler(int & _value) : value(_value) { }
~EndOfScopeDoubler() { value *= 2; }
private:
int & value;
};
int i = 7; // global variable
int f()
{
EndOfScopeDoubler blah(i);
return i;
}
int main(int argc, char ** argv)
{
std::cout << "i (before) = " << i << std::endl;
std::cout << "f() returns " << f() << std::endl;
std::cout << "i (after) = " << i << std::endl;
return 0;
}
i (before) should be 7. i (after) should be 14. But what should f
() return? It could return 7 or 14, depending on whether the
EndOfScopeDoubler destructor fires off before the return statement or
not.
When I run this program, I see:
i (before) = 7
f() returns 7
i (after) = 14
The fact that i ended up as 14 tells me that the EndOfScopeDoubler
destructor did fire off, but the fact that f() returned 7 tells me
that the value 7 got returned before the destructor got a chance to
modify it.
(Just so you know, if I surround the line "EndOfScopeDoubler blah
(i);" with curly braces, f() will return 14.)
My question is: Is having the return value returned before the
destructor(s) fire off defined behavior? Sure, my compiler happens to
think so, but I'm wondering if this is valid C++ behavior (or simply
undefined).
Thanks.
-- Jean-Luc