A much better implementation would be
using std::begin, std::end;
#define countof(x) (end(x) - begin(x))
(except for the double evaluation, but this is better as a template function instead of a macro anyway) because it'll work on any type that supports random-access iterators which include arrays but also std::array and vector.One of the uses for countof is (somewhat ironically) doing a enum->string table where you want to enforce the table gets manually updated properly:
enum Color { red, green, blue, num_colors };
const char *color_string[] = { "red", "green", "blue" };
static_assert(countof(color_string) == num_colors);Start ignoring here: Are you suggesting end() and begin() can be called on an array? As far as I know they can't. Apparently you can deduce array length in a constexpr function now in C++11 (which I only learned just now but also couldn't get working quickly, so have some salt with that), but before that arrays always degrade to pointers when passed as function arguments so there's (afaik) no way to extract their length from their type...
Here is the implementation of std::begin for arrays in libc++: https://github.com/llvm-mirror/libcxx/blob/60d223df071f6e3d4...
int a[] = {4,5,6};
for(int i : a)
;
That is how the above works.