在 'for' 语句的第二个 "parameter" 中声明的对象的生命周期
Life-time of object declared in the second "parameter" of 'for' statement
我刚刚发现也可以在 for 语句的第二个 "parameter" 中放置一个声明。但是我无法在任何地方找到它在该参数中声明的对象的 construction/destruction 方面的行为。
让我们用这个简单的代码:
struct C {
C() { puts("constr"); }
~C() { puts("destr"); }
};
int main() {
for (int i = 0; auto h = std::make_unique<C>(); i++) {
puts("in");
}
}
你能告诉我h
什么时候被销毁吗? (在 puts("in")
、i++
、...之后?)。它与 break;
和 continue;
的行为如何?
感谢您的澄清!
在循环条件中创建的对象的生命周期绑定到循环体的范围内,也可以在迭代表达式中使用(i++
在你的例子中)。条件在每次迭代开始时被评估,它创建的对象持续到该迭代结束,然后被销毁并为下一次迭代再次创建,依此类推。 break
或 continue
语句不会影响在条件中创建的对象的生命周期。
推理如下。从[stmt.for]可以看出,for循环是根据while循环定义的。
The for statement
for ( init-statement condition ; expression ) statement
is equivalent to
{
init-statement
while ( condition ) {
statement
expression ;
}
}
跳回到 [stmt.while] 然后得到你问题的答案(强调我的):
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. [...]]
想的简单,如果我是语言设计者,下面的语句:
for ( a; b; c ) d
和
for ( a; { b }; { c }) { d }
应该严格等价
很明显a
中定义的一些变量可以在b
,c
,d
中使用,所以只有a
比较特殊。
顺便说一句
我觉得Scala中的for-statement更漂亮。在 Scala 中 for
只是一个语法 suger:
for (i <- v) s <==> v.foreach(i => s)
在哪里
i => s
lamba 语法是这样的:
[](auto i){ s }
在 C++11 中
这是普遍正确的,定义函数foreach
的每个类型的变量v
都可以在for
语句中使用。
我刚刚发现也可以在 for 语句的第二个 "parameter" 中放置一个声明。但是我无法在任何地方找到它在该参数中声明的对象的 construction/destruction 方面的行为。
让我们用这个简单的代码:
struct C {
C() { puts("constr"); }
~C() { puts("destr"); }
};
int main() {
for (int i = 0; auto h = std::make_unique<C>(); i++) {
puts("in");
}
}
你能告诉我h
什么时候被销毁吗? (在 puts("in")
、i++
、...之后?)。它与 break;
和 continue;
的行为如何?
感谢您的澄清!
在循环条件中创建的对象的生命周期绑定到循环体的范围内,也可以在迭代表达式中使用(i++
在你的例子中)。条件在每次迭代开始时被评估,它创建的对象持续到该迭代结束,然后被销毁并为下一次迭代再次创建,依此类推。 break
或 continue
语句不会影响在条件中创建的对象的生命周期。
推理如下。从[stmt.for]可以看出,for循环是根据while循环定义的。
The for statement
for ( init-statement condition ; expression ) statement
is equivalent to
{ init-statement while ( condition ) { statement expression ; } }
跳回到 [stmt.while] 然后得到你问题的答案(强调我的):
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. [...]]
想的简单,如果我是语言设计者,下面的语句:
for ( a; b; c ) d
和
for ( a; { b }; { c }) { d }
应该严格等价
很明显a
中定义的一些变量可以在b
,c
,d
中使用,所以只有a
比较特殊。
顺便说一句
我觉得Scala中的for-statement更漂亮。在 Scala 中 for
只是一个语法 suger:
for (i <- v) s <==> v.foreach(i => s)
在哪里
i => s
lamba 语法是这样的:
[](auto i){ s }
在 C++11 中
这是普遍正确的,定义函数foreach
的每个类型的变量v
都可以在for
语句中使用。