13 ms·
0 is the value integral types get default initialized too, it's the equivalent of default initializing a boolean to false.
by celticmusic 7y ago
0 is the value integral types get default initialized too, it's the equivalent of default initializing a boolean to false.
- Sharlin 7y agoBut the fact that some types have a reasonable default value doesn’t make the values intrinsically equivalent to Boolean false. Plus, implicit default initialization is itself not necessarily a desirable feature. Especially not the kind that just default initializes to an all-zeroes bit pattern.
- celticmusic 7y agodefault initialization is a desirable feature in most circumstances, and I would argue most languages should be opt-out rather than opt-in. There are really only two reasons I can think of for not wanting default initialization. 1. performance 2. to catch mistakes on the edges of your system. A good example of the second is converting incoming data to enumerations. If you're not explicitly checking for it, you can accidentally default initialize to a valid enumeration. This is why I always set the first enumeration to 1 for any language that allows me to. If someone makes that mistake, the missing data causes an error rather than silently doing the wrong thing. But I can't really think of any reason outside of those two cases where you wouldn't want default initialization. And my point was that one of the reasons 0 is chosen as false because it's consistent with respect to default initialization of booleans. I realize that isn't the original reason, but today it's a good reason for having the behavior.