Strangest language feature
stackoverflow.com
stackoverflow.com
Funny enough, several people gave interesting places where this kind of syntax is a good thing. Goes to show that even meaningless pieces of fun on SO still end of enriching the reader.
Unfortunately, I can't really find it now - there is a link to a question which claims to have a use for this syntax, but it's been removed.
There are these 2 comments: "Don't forget "Hello World"[i]. Or i["Hello World"] – Richard Pennington Jan 3 '10 at 15:12"
"Or, more usefully, "0123456789abcdef"[x & 0xf] "
Their basic point is that you can use this trick to find the i'th character in a string. Which I guess is useful sometimes...
---
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.
I personally prefer sizeof(*(arr)), which is the same as arr[0], but I suppose C++ lets you overload that as well.
Oh well. If you happen to find the funny but semi-legitimate one, I'd love to see it.
It's rumored to have been written by either Kernighan or Ritchie.
register n = (count + 7) / 8;
switch(count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while(--n > 0);
}
[0] http://en.wikipedia.org/wiki/Duff%27s_deviceCan we please not turn this thread into a complaining session about SO's policies?
No, because every website has to be all things to all people, even if it means the destruction of the few things it was actually good at.
The last one makes me wonder a bit. Given local int variable x, what does this evaluate to?
(object) x.ToString() == (object) x.ToString()
When I run it, I get false, and the author says that it's false, because we're doing reference comparison of two independently created objects. But I thought strings were interned in .NET, so if you independently create 2 strings with the same contents, then they are actually the same object, which would make this true?
As an example:
(object)"XYZ" == (object)"XYZ"
returns true. So does:
(object) String.Intern(x.ToString()) == (object) String.Intern(x.ToString())
The trick is to know exactly which strings are interned, and when. I initially thought that all strings were, but it seems that string literals in code are always interned, and dynamically-created strings are mostly not interned automatically, but can be manually.
http://en.wikipedia.org/wiki/String_interning http://msdn.microsoft.com/en-us/library/system.string.intern...
OTOH, by that same logic there should have been less JavaScript representation, because JavaScript is downright goofy.
http://en.wikipedia.org/wiki/Call-with-current-continuation
The main thing which helped me get some kind of grip on it was having worked through longjmp/setjump in C some time ago.
Other than that, they are not a "strange language feature", they don't behave in unexpected or unclear ways.
While `!` is certainly a very special piece of erlang, its also not strange as well, as in say.... Ruby flipflops?
http://msdn.microsoft.com/en-us/library/aa266173(v=vs.60).as...