合法且安全。C99/C++ 標準允許在塊 (block) 內任意位置聲明變量。
while (cond) {
int x = compute(); // 每次迭代重新分配、初始化
// ...
} // x 在此銷毀
// 等價於
while (cond) {
{ // 隱式塊
int x = compute();
// ...
} // 隱式塊結束,x 銷毀
}
clear()/reset()for (int i=0; ...) 讓循環變量自動限制在循環作用域