It depends on the conditions.
If x
is zero-initialized, then padding has zero bits, so this case is well defined (8.5/6 of C++14):
To zero-initialize an object or reference of type T means:
— if T is a scalar type (3.9), the object is initialized to the value
obtained by converting the integer literal
0 (zero) to T;105
— if T is a (possibly cv-qualified) non-union class type, each
non-static data member and each base-class
subobject is zero-initialized and padding is initialized to zero bits;
— if T is a (possibly cv-qualified) union type, the object’s first
non-static named data member is zero-
initialized and padding is initialized to zero bits;
— if T is an array type, each element is zero-initialized; — if T is a
reference type, no initialization is performed.
However, if x
is default-initialized, then padding isn't specified, so it has indeterminate value (inferred by the fact that there's no mention of padding here) (8.5/7):
To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default
constructor (12.1) for T is called (and the initialization is
ill-formed if T has no default constructor or overload resolution
(13.3) results in an ambiguity or in a function that is deleted or
inaccessible from the context of the initialization);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.
And comparing indeterminate values is UB for this case, as none of the mentioned exceptions apply, as you compare the indeterminate value to something (8.5/12):
If no initializer is specified for an object, the object is
default-initialized. When storage for an object with automatic or
dynamic storage duration is obtained, the object has an indeterminate
value, and if no initialization is performed for the object, that
object retains an indeterminate value until that value is replaced
(5.17). [ Note: Objects with static or thread storage duration are
zero-initialized, see 3.6.2. — end note ] If an indeterminate value is
produced by an evaluation, the behavior is undefined except in the
following cases:
— If an indeterminate value of unsigned narrow character type (3.9.1)
is produced by the evaluation of:
......— the second or third operand of a conditional expression (5.16),
......— the right operand of a comma expression (5.18),
......— the operand of a cast or conversion to an unsigned narrow character type (4.7, 5.2.3, 5.2.9, 5.4),
or
......— a discarded-value expression (Clause 5), then the result of the
operation is an indeterminate value.
— If an indeterminate value of unsigned narrow character type is
produced by the evaluation of the right operand of a simple assignment
operator (5.17) whose first operand is an lvalue of unsigned narrow
character type, an indeterminate value replaces the value of the
object referred to by the left operand.
— If an indeterminate value of
unsigned narrow character type is produced by the evaluation of the
initialization expression when initializing an object of unsigned
narrow character type, that object is initialized to an indeterminate
value.