I have just discovered that it is possible to place a declaration also in the second "parameter" of a for-statement. But I was not able to find anywhere how it behaves with regard to the construction/destruction of the object declared in that parameter.
Let's have this simple code:
struct C {
C() { puts("constr"); }
~C() { puts("destr"); }
};
int main() {
for (int i = 0; auto h = std::make_unique<C>(); i++) {
puts("in");
}
}
Please could you tell me when h
is destroyed? (after puts("in")
, i++
, ...?). How does it behave with break;
and continue;
?
Thanks for clarification!
In object-oriented programming (OOP), the object lifetime (or life cycle) of an object is the time between an object's creation and its destruction.
The lifetime of a variable defines the duration for which the computer allocates memory for it (the duration between allocation and deallocation of memory). In C language, a variable can have automatic, static or dynamic lifetime. Automatic − A variable with automatic lifetime are created.
The space for the static variable is allocated only one time and this is used for the entirety of the program. Once this variable is declared, it exists till the program executes. So, the lifetime of a static variable is the lifetime of the program.
The lifetime of a variable is the time during which the variable stays in memory and is therefore accessible during program execution. The variables that are local to a method are created the moment the method is activated (exactly as formal parameters) and are destroyed when the activation of the method terminates.
The lifetime of the object created within the loop condition is bound to the scope of the loop body, and can also be used within the iteration expression (i++
in your example). The condition is evaluated at the beginning of each iteration, the object it creates lasts until the end of that iteration, is then destroyed and created again for the next iteration, and so on. break
or continue
statements do not influence the lifetime of the object created in the condition.
The reasoning is as follows. From [stmt.for], we can see that a for loop is defined in terms of a while loop.
The for statement
for ( init-statement condition ; expression ) statement
is equivalent to
{ init-statement while ( condition ) { statement expression ; } }
Jumping back to [stmt.while] then yields the answer to your question (emphasis mine):
When the condition of a while statement is a declaration, the scope of the variable that is declared extends from its point of declaration ([basic.scope.pdecl]) to the end of the while statement. A while statement whose condition is an initialized declaration of some variable t is equivalent to
label: { // start of condition scope condition; // declares t if (t) { statement goto label; } } // end of condition scope
[ Note: The variable created in the condition is destroyed and created with each iteration of the loop. [...]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With