2 ms·
It also probably shouldn't (warn) if(Foo* a = GetAFoo()) { /* Do something with a non-null foo */ } is perfectly valid code.
by maccard 10y ago
It also probably shouldn't (warn)
if(Foo* a = GetAFoo()) { /* Do something with a non-null foo */ }
is perfectly valid code.
- gnaritas 10y agoDepends on the language, C# won't allow that, if's don't accept null, they accept bool.
- maccard 10y agoTrue. I was speaking of C++, which also accepts bool [0] (or more specifically, something that can be converted to a bool, which a null pointer can be [1]). Actually [0] has a good example of when the if (Foo* a = ...) syntax is useful (dynamic cast) [0] http://en.cppreference.com/w/cpp/language/if http://en.cppreference.com/w/cpp/language/if [1] https://ideone.com/im4uVn https://ideone.com/im4uVn
- kccqzy 10y agoI believe that's a declaration inside `if`. C doesn't support that, but C++ does. I believe in this case you can't actually put parenthesis around it.