[bind10-dev] Enums as array indices and manual vs. auto-numbering

Michal 'vorner' Vaner michal.vaner at nic.cz
Thu Aug 25 11:50:39 UTC 2011


Hello

AFAIK our code guideline suggests that whenever we write an enum, we should
manually assign numbers to it:

enum X {
  A = 0,
  B = 1
};

However, we sometime use an enum as constants for named indices of array:

enum Indices {
  FOO = 0,
  BAR = 1,
  BAZ = 2,
  COUNT = 3
}
int array[COUNT];

array[FOO] = 42;


I don't know the rationale behind numbering them manually, but in this case I
think it is error prone, because human will just overlook it and assign bad
number. If we rely on the compiler auto-assigning, C and C++ guarantees
numbering them from zero in by-one increasing way (the rules are more
complicated, but in case it's left completely on the compiler, this simple one
follows from it). In this case I believe the auto-assigning is actually safer.

What reason do I overlook? Or should we revisit the code guidelines?

Thank you

With regards

-- 
Never underestimate the bandwidth of a station wagon full of HDDs.

Michal 'vorner' Vaner
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <https://lists.isc.org/pipermail/bind10-dev/attachments/20110825/92e7a4c0/attachment.bin>


More information about the bind10-dev mailing list