变量是否只能在if语句的范围内定义,类似于for-loops的常用做法? [英] Can a variable be defined only in the scope of an if-statement, similar to how it's often done for for-loops?

查看:147
本文介绍了变量是否只能在if语句的范围内定义,类似于for-loops的常用做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在if结构中声明,分配和比较一个变量到表达式,以便只在if结构的范围内定义?

Is there a way to declare, assign and compare a variable to an expression in an if construction in such a way that it is only defined in the scope of the if construction?

这个工作(声明和赋值,当然条件只是函数f的返回值等于零):

This works (declare and assign, but of course the condition is only the return value of function f being equal to zero or not):

int main()
{
  if(int i = f())
  {
    printf("%d", i);
    // i is defined here!
  }
  else
  {
    // Here too!
  }
  // But not here!
}

但是当我尝试比较i的值和实际表达式时麻烦:

But when I try to compare the value of i with an actual expression I run into trouble:

int main()
{
  // Does not compile because "int i = f()" is not a primary expression?
  if((int i = f()) == 3)
  {
    printf("%d", i);
  }
}

如果建筑类型从行为的角度看,但是它看起来很丑陋:

Creating a scope around the whole if construction kind of does the trick, from a behavior point of view, but it looks ugly in my opinion:

int main()
{
  {
    int i = f();
    if(i == 3)
    {
      printf("%d", i);
      // i is defined here!
    }
    // here too!
  }
  // i is not defined here!
}

我比较for-loops和switch的外观和感觉它是如此整洁地声明和分配一个变量,所以它只在所讨论的范围内定义。当然,变量的值不会与任何东西进行比较,我不是说它是一样的东西:

I'm comparing with the look and feel of for-loops and switches where it's so neat to declare and assign a variable so it is only defined in the scope in question. Of course the value of the variable is not compared to anything there and i'm not saying it's the same thing:

for(int i = 0;;)
{
  break;
}
// i is not defined here

switch(int i = f())
{
  default: break;
}
// i is not defined here!

所以总结一下,是否有一种方法可以将变量的定义绑定到if类​​似的方式,因为它通常绑定的for和(可能不是普通)切换的范围,你实际上比较变量的值作为if语句的条件的表达式

So to sum it up, is there a way to tie the definition of a variable to the scope of "if" in a similar way as it's commonly tied to the scope of for and (perhaps not as commonly) switch, and where you actually compare the value of the variable to an expression as the condition of the if-statement?

推荐答案

据我所知,在条件中没有办法同时有一个声明和一个 if语句。如果我们看一下C ++标准部分 6.4 的选择语句,则 if 的语法如下:

As far as I can tell there is no way to have both a declaration and an expression within the condition of an if statement. If we look at the draft C++ standard section 6.4 Selection statements the grammar for if is as follows:

selection-statement:
    if ( condition ) statement
    if ( condition ) statement else statement
    switch ( condition ) statement
condition:
    expression
    attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
    attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list

因此,您可以使用

你在替代方法中提出的,声明 i 之前的 if语句似乎是最好的选择。虽然似乎不需要使用包围块:

What you proposed in the alternative, declaring i before the if statement seems like the best option. Although using an enclosing block does not seem necessary:

int i = f();
if(i == 3)

这篇关于变量是否只能在if语句的范围内定义,类似于for-loops的常用做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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