News for hackers. Stuff that matters.
Re: Strangest language feature by derobert on Monday March 25, 2013 @02:28PM (#5438693)
The answer on the deleted question (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.

Re: Strangest language feature by mikeash on Monday March 25, 2013 @02:40PM (#5438775)
Ah! 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.