开发者

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

开发者 https://www.devze.com 2023-01-04 04:31 出处:网络
Is this well defined? Streamreader ^reader = gcnew Streamreader(\"test.txt\"); String^line; while ((line = reader->ReadLine()) != nullptr && line != \"\")

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

精彩评论

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