C ++ 11, 6.4 / 4:
The value of a condition that is an expression is the value of an expression contextually converted to bool for statements other than a switch; if this transformation is poorly formed, the program is poorly formed.
Thus, the standard says that the compiler must perform any implicit conversions at its disposal in order to convert the array to logical. Expanding the array to a pointer and converting the pointer to boolean with a test against equality to zero is one way to do this, so yes, the program is well defined and yes, it gives the correct result - obviously, since the array is allocated on the stack, the pointer that it splits up, can never be equal to a null pointer.
Update: Regarding this chain of two transformations:
C ++ 11, 4.2 / 1:
An array of NT or an array of unknown bounds of T lvalue or rvalue type can be converted to a pointer to T type prvalue. The result is a pointer to the first element of the array.
So, the only legal conversion from an array type is a pointer to an element type. There is no choice at the first stage.
C ++ 11, 4.12 / 1:
Arithmetic value, unlisted enumeration, pointer or a member type pointer can be converted to a prvalue of type bool . Zero value, null pointer value or null element pointer value is converted to false ; any other value is converted to true . A value of type std::nullptr_t can be converted to prvalue of type bool ; The resulting value is false .
There is an implicit conversion directly from the bare pointer to boolean; therefore, the compiler chooses this as the second step, because it allows you to immediately get the desired result (conversion to logical).
source share