为什么我可以在循环内重新初始化常量?
Why can I reinitialize a constant inside a loop?
编译器不会对以下代码发出警告或错误。 const 限定符的含义是否被滥用?显然我不能稍后在同一个循环迭代中重新分配它,但它似乎在每次迭代后重新分配它。
示例代码:
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
您没有重新初始化它,您只是在每个循环迭代中初始化它* 。正式地说,在每个循环迭代中都会创建和销毁一个新的 int
,尽管编译器可以做任何它想做的事情,只要它看起来像那样。
* 在 C++ 中你真的不能 "re-initialize" 事情,初始化在一个对象的生命周期中只发生一次
这个变量在每次迭代时都会被初始化和销毁,因为它是循环的本地。
您实际上并不是在此处重新初始化。每次循环都会创建一个新变量。
constant
局部于循环内部的块。当块在给定迭代中完成并且控制返回到 for
时,constant
超出范围,因此不再存在。当 for
开始循环的下一次迭代时,将创建并初始化 constant
的新实例。
如果遵循C标准那么(6.2.4对象的存储时长)
1 An object has a storage duration that determines its lifetime.
There are four storage durations: static, thread, automatic, and
allocated. Allocated storage is described in 7.22.3.
和
5 An object whose identifier is declared with no linkage and without
the storage-class specifier static has automatic storage duration,
as do some compound literals. The result of attempting to indirectly
access an object with automatic storage duration from a thread other
than the one with which the object is associated is
implementation-defined.
6 For such an object that does not have a variable length array type,
its lifetime extends from entry into the block with which it is
associated until execution of that block ends in any way. (Entering an
enclosed block or calling a function suspends, but does not end,
execution of the current block.) If the block is entered
recursively, a new instance of the object is created each time. The
initial value of the object is indeterminate. If an initialization is
specified for the object, it is performed each time the declaration or
compound literal is reached in the execution of the block; otherwise,
the value becomes indeterminate each time the declaration is reached
最后(6.8.5 迭代语句)
5 An iteration statement is a block whose scope is a strict subset of
the scope of its enclosing block. The loop body is also a block
whose scope is a strict subset of the scope of the iteration
statement.
因此在这个循环语句中
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
循环体是一个块。变量 constant
具有自动存储期限。每次递归执行块时都会创建一个新的变量实例。
在 C++ 中,您可以添加存储 class 说明符 static
。在这种情况下,变量确实只会被初始化一次,因为它具有静态存储持续时间(在 C 中你可能不会这样做,因为变量必须由常量表达式初始化)。
这是一个演示程序
#include <iostream>
int main()
{
for ( int i = 0; i < 10; ++i )
{
static const int constant = i;
std::cout << "constant = " << constant << std::endl;
}
return 0;
}
它的输出是
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
编译器不会对以下代码发出警告或错误。 const 限定符的含义是否被滥用?显然我不能稍后在同一个循环迭代中重新分配它,但它似乎在每次迭代后重新分配它。
示例代码:
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
您没有重新初始化它,您只是在每个循环迭代中初始化它* 。正式地说,在每个循环迭代中都会创建和销毁一个新的 int
,尽管编译器可以做任何它想做的事情,只要它看起来像那样。
* 在 C++ 中你真的不能 "re-initialize" 事情,初始化在一个对象的生命周期中只发生一次
这个变量在每次迭代时都会被初始化和销毁,因为它是循环的本地。
您实际上并不是在此处重新初始化。每次循环都会创建一个新变量。
constant
局部于循环内部的块。当块在给定迭代中完成并且控制返回到 for
时,constant
超出范围,因此不再存在。当 for
开始循环的下一次迭代时,将创建并初始化 constant
的新实例。
如果遵循C标准那么(6.2.4对象的存储时长)
1 An object has a storage duration that determines its lifetime. There are four storage durations: static, thread, automatic, and allocated. Allocated storage is described in 7.22.3.
和
5 An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration, as do some compound literals. The result of attempting to indirectly access an object with automatic storage duration from a thread other than the one with which the object is associated is implementation-defined.
6 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached
最后(6.8.5 迭代语句)
5 An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.
因此在这个循环语句中
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
循环体是一个块。变量 constant
具有自动存储期限。每次递归执行块时都会创建一个新的变量实例。
在 C++ 中,您可以添加存储 class 说明符 static
。在这种情况下,变量确实只会被初始化一次,因为它具有静态存储持续时间(在 C 中你可能不会这样做,因为变量必须由常量表达式初始化)。
这是一个演示程序
#include <iostream>
int main()
{
for ( int i = 0; i < 10; ++i )
{
static const int constant = i;
std::cout << "constant = " << constant << std::endl;
}
return 0;
}
它的输出是
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0