Posted at

【C++】`if(pointer != NULL)`の代わりに、`if(pointer)`が使えるよ

More than 3 years have passed since last update.

null pointerはbooleanのfalseと解釈され、non-null pointerはtrueと解釈されるため、

if(pointer != NULL)の代わりに、if(pointer)`が使える。


参考: From the C++11 standard, section on Boolean Conversions:

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true . A prvalue of type std::nullptr_t can be converted to a prvalue of type bool ; the resulting value is false .