第一个条件中的变量赋值和第二个条件中使用相同的变量 定义良好吗?
这是明确定义的吗?
Streamreader ^reader = gcnew Streamreader("test.txt");
String ^line;
while ((line = reader->ReadLine()) != nullptr && line != "")
{
//do stuff
}
我相信我在某处读到不能保证赋值在第二个条件之前执行。可能是我错了或者这仅适用于 C。
谷歌没有帮助我解决这个问题,这就是我在这里问的原因:)
Is this well defined?
Streamreader ^reader = gcnew Streamreader("test.txt");
String ^line;
while ((line = reader->ReadLine()) != nullptr && line != "")
{
//do stuff
}
I believe that I read somewhere that it is not guaranteed that the assignment is executed before the 2nd conditional. It may be that I'm wrong or that this just applies for C.
Google did not help me with this, that is why I am asking here :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 &&和||,保证在评估第二个条件之前评估第一个条件(包括赋值)。
与按位 &而|,另一方面,不提供此类保证。
With && and ||, it is guaranteed to evaluate the first condition (including the assignment) before evaluating the second condition.
With bitwise & and |, on the other hand, no such guarantees are made.
这里有一个相关的答案,其中有许多很好的参考:是否强制要求短路逻辑运算符?以及评估顺序?
如果您没有重载 && ,请简短回答。和 ||您将得到从左到右的短路评估。查看链接。
There is a related answer here with a number of good references: Is short-circuiting logical operators mandated? And evaluation order?
Short answer if you haven't overloaded && and || you'll get short-circuit evaluation, which goes from left to right. Take a look in the link.