3 ms·
The answer on the deleted question (http://stackoverflow.com/questions/469696/what-is-your-most-useful-c-c-utility/470221#470221 http://stackoverflow.com/questi
by derobert 14y ago
The answer on the deleted question (http://stackoverflow.com/questions/469696/what-is-your-most-useful-c-c-utility/470221#470221 http://stackoverflow.com/questions/469696/what-is-your-most-...), by Josh Kelley, gives this:
---
Get the number of elements in an array:
#define ITEMSOF(arr) (sizeof(arr) / sizeof(0[arr]))
(0[arr] is identical to arr[0] for arrays but will intentionally fail if it's used against a C++ object that overloads operator[].)
He gives a C++ template version, too.
---
... 10k+ users on Stack Overflow can see deleted stuff. Or you can probably get them out of the CC data dumps (but that's a lot more work than you want, probably). Or maybe out of the data explorer.
- mikeash 14y agoAh! I saw that #define but didn't see the bit about failing on C++ objects that overload operator[]. I don't do C++ much so that really didn't occur to me. I personally prefer sizeof(*(arr)), which is the same as arr[0], but I suppose C++ lets you overload that as well.