The compiler throws no warnings or errors for the following code. Is the meaning of the const qualifier being abused? Obviously I cannot reassign it later in the same loop iteration but it does seem to reassign it after each iteration.
Sample code:
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
You aren't re-initializing it, you're just initializing it in each loop iteration*. Formally there is a new int
being created and destroyed in each loop iteration, although the compiler can do whatever it wants as long as it seems to behave that way.
* You can't really "re-initialize" things in C++, initialization only happens once in the lifetime of an object
If to follow the C Standard then (6.2.4 Storage durations of objects)
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.
and
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
And at last (6.8.5 Iteration statements)
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.
Thus in this loop statement
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
the body of the loop is a block. The variable constant
has the automatic storage duration. A new instance of the variable is created each time the block is executed recursively.
In C++ you may add storage class specifier static
. In this case the variable indeed will be initialized only once because it has the static storage duration ( In C you may not do the same because the variable must be initialized by a constant expression).
Here is a demonstrative program
#include <iostream>
int main()
{
for ( int i = 0; i < 10; ++i )
{
static const int constant = i;
std::cout << "constant = " << constant << std::endl;
}
return 0;
}
Its output is
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
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