xmllmx
xmllmx

Reputation: 42371

Why does C++11 define a non-existing value as a default value?

Below is excerpted from cppreference link 1 and cppreference link 2:

enum codecvt_mode
{
    consume_header = 4,
    generate_header = 2,
    little_endian = 1
};

template
<
    class Elem,
    unsigned long Maxcode = 0x10ffff,
    std::codecvt_mode Mode = (std::codecvt_mode)0
>
class codecvt_utf8_utf16 : public std::codecvt<Elem, char, std::mbstate_t>;

Obviously, 0 is not a well-defined value of enum type codecvt_mode. The C++ standard might intentionally leave the default value implementation-defined.

However, I think an implementation-defined default value can be vague to the user.

Why does C++11 define

std::codecvt_mode Mode = (std::codecvt_mode)0

rather than

std::codecvt_mode Mode = std::little_endian

?

Upvotes: 1

Views: 110

Answers (2)

AnT stands with Russia
AnT stands with Russia

Reputation: 320541

0 is a valid value for your enum type. According to language standard enum types whose underlying type is not fixed (your case) can represent all integer values representable in a bit-field of sufficient width (sufficient to represent all named enum members you declared). The fact that you sometimes need an explicit cast to force all these value into that enum type is just a little inconvenience. This means that 0 is a valid value for any enum type.

Upvotes: 6

ShadowRanger
ShadowRanger

Reputation: 155438

The enumeration is a set of bit flags that can be combined (via bitwise operations), not values that are selected independently. A value of zero means none of the flags are active (which makes it big endian, and does not consume or generate a BOM).

Upvotes: 5

Related Questions