在 if 条件内声明变量有什么问题?
也许我已经生疏了(最近一直在用Python编写)。
为什么这不能编译?
if ( (int i=f()) == 0)
如果没有 ()
周围的 int i=f()
我得到另一个更合理的错误 i
is not being boolean。但这就是为什么我首先想要括号!
我的猜测是,使用括号将其放入表达式中,并且表达式中不允许使用声明语句。是这样吗?如果是的话,这是 C++ 的语法怪癖之一吗?
顺便说一句,我实际上是想这样做:
if ( (Mymap::iterator it = m.find(name)) != m.end())
return it->second;
Perhaps I am getting rusty (have been writing in Python recently).
Why does this not compile?
if ( (int i=f()) == 0)
without the ()
around the int i=f()
I get another, much more reasonable error of i
is not being boolean. But that's why I wanted the parentheses in the first place!
My guess would be that using the parentheses makes it into an expression, and that declaration statements are not allowed in an expression. Is it so? And if yes, is it one of the C++'s syntax quirks?
BTW, I was actually trying to do this:
if ( (Mymap::iterator it = m.find(name)) != m.end())
return it->second;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 C++ 中的
if
语句中声明变量,但它仅限于直接初始化,并且需要转换为布尔值:C++ 没有任何可以描述为“声明表达式”,即声明变量的[子]表达式。
实际上,我只是查了一下标准中的条款,根据 6.4 [stmt.select] 第 1 段,两种形式的初始化都支持:
也就是说,也可以写:
显然,这仅适用于 C++2011因为C++2003没有大括号初始化。
You can declare a variable in the
if
statement in C++ but it is restricted to be used with direct initialization and it needs to convert to a Boolean value:C++ doesn't have anything which could be described as "declaration expression", i.e. [sub-] expressions declaring a variable.
Actually, I just looked up the clause in the standard and both forms of initialization are supported according to 6.4 [stmt.select] paragraph 1:
That is, it is also be possible to write:
Obviously, this only works in C++2011 because C++2003 doesn't have brace-initialization.
范围有问题。
考虑以下代码:
b 是否在块内声明?如果
foo1()
返回 true 会怎样?There's a problem with scope.
Consider the following code:
Is b declared inside the block? What if
foo1()
returns true?您可以在 if 语句(或在 for 或 while 中)声明变量,但只能在外括号块中声明,并且它需要能够转换为 bool。
您的猜测基本上是正确的,这是不允许的,因为
不是带有初始化的有效声明。
你需要额外的一行,
但最好写成
You can put the
return
line after theif
,如果你真的想要这行回来,至少对我来说这不会不会损害可读性,但其他人可能会认为不同。如果您真的非常想声明一个迭代器并在
if
条件中将其用作 bool ,您可以这样做,但我认为这有害;-)
You can declare a variable in an if statement (or in for or while), but only in the outer parenthesis block and it needs to be able to be converted to bool.
Your guess is basically right, it's not allowed because
is not a valid declaration with initialization.
You need one additional line,
but then it's better to write
You can put the
return
line after theif
, if you really want this line back, at least for me this doesn't harm readability, but others might see that different.If you really, really want to declare an iterator and use the it as bool in an
if
condition, you could dobut I would consider this harmful ;-)
确实,你不会写,
但你可以完美地写
所以你可以使用
&&
运算符在一个语句中执行这两个操作,例如i
应该初始化为0 以外的任何值,如果您的编译器应用从左到右的计算,则它应该是第一个条件。但不幸的是,正如第二个示例所要求的那样,这不适用于迭代器。
It's true that you can't write
but you can perfectly write
So you can use the
&&
operator to perform both operations in one statement likei
should be initialized with any value other than 0, and it should be the first condition if your compiler applies left-to-right evaluation.But unfortunately, that's not applicable in case of iterators as your second example asks.