4 ms·
Today I learned... _magic_ #include <cstdio> void sw(int s) { switch (s) while (0) { case 0: printf("zero\
by inetknght 7y ago
Today I learned... _magic_
#include <cstdio>
void sw(int s)
{
switch (s) while (0) {
case 0:
printf("zero\n"); continue;
case 1:
printf("one\n"); continue;
case 2:
printf("two\n"); continue;
}
}
[0] https://gcc.godbolt.org/z/Q26LWG https://gcc.godbolt.org/z/Q26LWG
- lostmsu 7y agoHuh, what does it do?
- strbean 7y agoLooking at the link, it generates the exact same assembly (and behavior of course) as void sw(int s) noexcept { switch (s) { case 0: printf("zero\n"); break; case 1: printf("one\n"); break; case 2: printf("two\n"); break; } }
- saagarjha 7y agothatsthejoke.jpg
- strbean 7y agoI mean, parent seemed an awful lot like someone asking what it did. Damn me for missing the invisible /s. I'm sure no other reader would ever be curious!
- saagarjha 7y agoIt's not the comment that is the joke, but the example in the context of the "C obfuscation tricks" article where it's just a convoluted way to write the same thing as a simple C construct.
- inetknght 7y agoYup. Of all of the examples on that page, this is the only one new to me. I thought it pretty clever and after toying with it, it does seem pretty cool with functions as the conditionals.
- lostmsu 7y agoIs noexcept here the consequence of while? Or noexcept simply does nothing at all? Not a C expert, so just curious.
- eMSF 7y agoAFAIK noexcept is a C++ specifier that isn't valid C. Even so, it has nothing to do with the loop, but in C++ it would cause the program to terminate if an exception occurred in the sw function.
- MauranKilom 7y agoHey wait, that's just Duff's... Oh.