嵌套循环中的 C++ 变量重新声明
C++ variable redeclaration within nested loops
下面是一段包含 3 个嵌套循环的代码。变量 curs 和 tempv 在最外层的 for 循环的每次迭代中都被重新声明。这应该给我错误,但我能够 运行 它成功地在 gcc 4.8.4 上没有错误。
for(int i = 0; i<lend; i++)
{
string curs = vstring[digits[i]-'0'];
vector<string> tempv;
for(int j = 0; j<v.size(); j++)
{
for(int k = 0; k<curs.size(); k++)
{
tempv.push_back(v[j] + curs[k]);
}
}
v = tempv;
}
在 for 循环中重新声明变量可以吗?我知道在 C++ 中不能在同一范围内重新声明变量。
Is it fine to redeclare variables within for-loop? I have an understanding that in C++ a variable cannot be redeclared in the same scope.
这是一个单一的词法作用域,这些变量在其中声明一次。执行重复进入它的事实是无关紧要的——这不像是在多次调用的函数中声明的变量出现重复定义错误。与往常一样,每次您进入范围时,您都会获得其中定义的变量的新实例化。
换句话说:作用域规则是关于程序的静态、词法结构的,与程序的执行流程实际如何发生无关;它们充当其运行时行为的蓝图,因为它们指示编译器如何解析块的其余部分以及每次进入范围时生成什么代码。
这是完全合法的,事实上,它一直被使用。在这种情况下,每次迭代都可以看作是一个全新的范围。
” Variables curs
and tempv
are getting redeclared at each iteration of the outermost for-loop
不,它们在每次迭代时都会实例化。创建,并在块的末尾销毁。编译器完成的模优化。
作用域是一个编译时概念。您通过检查代码看到的东西。实例化是一种 运行 时间效应。相同的变量声明可以执行任意次,实例化该变量的次数。好吧,除非它是 static
,在这种情况下它被实例化一次。
” I have an understanding that in C++ a variable cannot be redeclared in the same scope.
不能在同一范围内直接重新声明,但可以在嵌套范围中重新声明。而且您有许多嵌套范围。让我们考虑一下它们:
for(int i = 0; i<lend; i++) // ← For loop scope.
{ // ← Block scope.
string curs = vstring[digits[i]-'0'];
vector<string> tempv;
for(int j = 0; j<v.size(); j++) // ← For loop scope.
{ // ← Block scope.
for(int k = 0; k<curs.size(); k++) // ← For loop scope.
{ // ← Block scope.
tempv.push_back(v[j] + curs[k]);
}
}
v = tempv;
}
形式上,for
循环由 定义 对应的 while
循环放置在包含循环变量声明的外部块中, 如果有的话。然而,
C++17 §6.3.3/1:
” Names declared in the init-statement, the for-range-declaration, and in the condition of if
, while
, for
, and
switch
statements are local to the if
, while
, for
, or switch
statement (including the controlled statement),
and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for
the if
statement, any of the outermost blocks) of the controlled statement; see 9.4.
例如,在您的代码中,外部 for
循环的 i
不能直接在构成该循环主体的块中重新声明,即使它是一个嵌套范围,但可以在内部嵌套循环。
下面是一段包含 3 个嵌套循环的代码。变量 curs 和 tempv 在最外层的 for 循环的每次迭代中都被重新声明。这应该给我错误,但我能够 运行 它成功地在 gcc 4.8.4 上没有错误。
for(int i = 0; i<lend; i++)
{
string curs = vstring[digits[i]-'0'];
vector<string> tempv;
for(int j = 0; j<v.size(); j++)
{
for(int k = 0; k<curs.size(); k++)
{
tempv.push_back(v[j] + curs[k]);
}
}
v = tempv;
}
在 for 循环中重新声明变量可以吗?我知道在 C++ 中不能在同一范围内重新声明变量。
Is it fine to redeclare variables within for-loop? I have an understanding that in C++ a variable cannot be redeclared in the same scope.
这是一个单一的词法作用域,这些变量在其中声明一次。执行重复进入它的事实是无关紧要的——这不像是在多次调用的函数中声明的变量出现重复定义错误。与往常一样,每次您进入范围时,您都会获得其中定义的变量的新实例化。
换句话说:作用域规则是关于程序的静态、词法结构的,与程序的执行流程实际如何发生无关;它们充当其运行时行为的蓝图,因为它们指示编译器如何解析块的其余部分以及每次进入范围时生成什么代码。
这是完全合法的,事实上,它一直被使用。在这种情况下,每次迭代都可以看作是一个全新的范围。
” Variables
curs
andtempv
are getting redeclared at each iteration of the outermost for-loop
不,它们在每次迭代时都会实例化。创建,并在块的末尾销毁。编译器完成的模优化。
作用域是一个编译时概念。您通过检查代码看到的东西。实例化是一种 运行 时间效应。相同的变量声明可以执行任意次,实例化该变量的次数。好吧,除非它是 static
,在这种情况下它被实例化一次。
” I have an understanding that in C++ a variable cannot be redeclared in the same scope.
不能在同一范围内直接重新声明,但可以在嵌套范围中重新声明。而且您有许多嵌套范围。让我们考虑一下它们:
for(int i = 0; i<lend; i++) // ← For loop scope.
{ // ← Block scope.
string curs = vstring[digits[i]-'0'];
vector<string> tempv;
for(int j = 0; j<v.size(); j++) // ← For loop scope.
{ // ← Block scope.
for(int k = 0; k<curs.size(); k++) // ← For loop scope.
{ // ← Block scope.
tempv.push_back(v[j] + curs[k]);
}
}
v = tempv;
}
形式上,for
循环由 定义 对应的 while
循环放置在包含循环变量声明的外部块中, 如果有的话。然而,
” Names declared in the init-statement, the for-range-declaration, and in the condition of
if
,while
,for
, andswitch
statements are local to theif
,while
,for
, orswitch
statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for theif
statement, any of the outermost blocks) of the controlled statement; see 9.4.
例如,在您的代码中,外部 for
循环的 i
不能直接在构成该循环主体的块中重新声明,即使它是一个嵌套范围,但可以在内部嵌套循环。