开发者

Variable assignment in 1st condition and using same variable in 2nd condition Well defined?

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 conditiona开发者_JS百科l. 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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜