do-while语句的主体中的声明范围 [英] Scope of declarations in the body of a do-while statement

查看:144
本文介绍了do-while语句的主体中的声明范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能在do while循环中声明变量? OP询问为什么在do-while循环的while-condition不在do-statement中的范围内。这将是非常不自然的,因为C / C ++通常遵循顶部声明模式。但是反过来 - 为什么不将do语句中的任何声明的范围扩展到while条件。这将允许

In Why can't you declare a variable inside a do while loop? the OP asks why a declaration in the while-condition of a do-while loop isn't in scope in the do-statement. That would be very unnatural as C/C++ generally follow a "declaration-at-top-of-scope" pattern. But what about the converse - why not extend the scope of any declaration in the do-statement to the while-condition. That would allow

int i;
do {
  i = get_data();
  // whatever you want to do with i;
} while (i != 0);

缩短为

do {
  int i = get_data();
  // whatever you want to do with i;
} while (i != 0);

它提供了用于限制控制变量范围的整洁的语法。 (注意:我知道这不是有效的语法 - 这就是问题的关键点,为什么不扩展语言允许这种语法。)

which gives a tidy syntax for limiting the scope of the control variable. (Note: I'm aware that this isn't valid syntax - that's the point of the question, why not extend the language to allow this syntax.)

注意在下面的注释中,这个扩展不会破坏现有的代码,并将非常的精神引入for-init(和while-init)作用域。

As I note in a comment below, this extension would not break existing code, and would be very much in then spirit of the introduction of for-init (and while-init) scoping.

推荐答案

现在作为一个do while存在的循环体是一个块:

So currently as a the do while exists the body of the loop is a block:

do 
{  // begin block
   int i = get_data();
  //    whatever you want to do with i;
}  //end block
while (i != 0);

因此块范围如何工作,从 code> 封锁范围

so how does a block scope work, from section 3.3.3 Block scope:


在块中声明的名称块;它具有块范围。它的潜在范围从
声明点开始(3.3.2),结束于其块的结尾。在块范围声明的变量是一个局部
变量。

A name declared in a block (6.3) is local to that block; it has block scope. Its potential scope begins at its point of declaration (3.3.2) and ends at the end of its block. A variable declared at block scope is a local variable.

是块,所以你想要一个规则,将创建一些特殊的do while块范围。这将如何实际工作?看起来最简单的方法是将声明提升为新创建的外部块:

So clearly the scope of i is the block, so you want a rule that would create some sort of special do while block scope. How would that practically work? It would seem the simplest equivalent would be to hoist the declaration to a newly created outer block:

{
  int i = get_data(); // hoist declaration outside
  do 
  {  // begin block
    //    whatever you want to do with i;
  }  //end block
  while (i != 0);
}

这可能会正常工作,如果所有的声明, ,但是像这样的情况:

which could possibly work fine if all the declarations where at the start of the body, but what about a scenario like this:

int k = 0 ;
do 
{
   int i = get_data();
   k++ ;        // side effect before declaration
   int j = k ;  // j it set to 1
}
while (i != 0);

int k = 0 ;
{
  int i = get_data();
  int j = k ;  // j is set to 0
  do 
  {
     k++ ;   
  }
  while (i != 0);
}

另一个替代方法是从do开始扩展范围,打破各种预期的行为。它会破坏 3.3.10 名称隐藏区块范围中的隐藏名称的功能:

Another alternative would be to extend the scope from the do while onwards but that would break all sorts of expected behavior. It would break how name hiding works in block scope from section 3.3.10 Name hiding:


名称可以通过在嵌套声明区域或派生
类(10.2)中显式声明该相同名称来隐藏。

A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class (10.2).

以及 3.3.1 中的示例显示:

int j = 24;
int main() {
    int i = j, j;
    j = 42;
}

main 隐藏全局 j ,但是如何为我们的特殊做范围工作?:

the j in main hides the global j but how would work for our special do while scope?:

int j = 24;
do {
    int i = j, j;
    j = 42;
} while( j != 0 )

扩展内部的范围 j 从do do向前肯定会打破很多现有的代码,并看着前面的例子没有直观的方式来提升声明。

Extending the scope of the inner j from the do while forwards would surely break a lot of existing code and looking at the previous example there is no intuitive way to hoist the declaration out.

我们可以最终玩这个游戏,找到一些有用的东西,但似乎很少的工作,很少的收获,这是完全不直观。

We could play with this an eventually and find something that works but it seems like a lot of work for very little gain and it is completely unintuitive.

这篇关于do-while语句的主体中的声明范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆