While working with some custom comparators, I encountered the need for a type that only had a single possible value. There are types like std::nullptr_t and empty structs where this is the case.
Then I considered the possibility of using an enum. I might declare an enum with a single value, something like
enum E
{
only_value // BUT IS IT??
};
But it seems that the standard says that all of the underlying type's values, which fit into the "smallest bitfield" that can contain the declared values, are valid.
From cppreference.com:
(The source value, as converted to the enumeration's underlying type if floating-point, is in range if it would fit in the smallest bit field large enough to hold all enumerators of the target enumeration.)
If you declare an enum with only a single enumerator, the smallest it can be is one bit. Following that logic, the unnamed enumerator with the bit's other value should be legal. If an enum is based on a signed integer, then -1 and 0 are always legal. On an unsigned integer, 0 and 1 are always legal.
Is there something else in the standard that makes the unnamed bit value illegal or UB?