On the indexing of arrays and other little wonders
-
@kshegunov said:
By the way, the proper way to unnecessarily make things look unfathomable is to use fully qualified access:
std::cout << a.operator[] (0) << std::endl;
Well, that won't work with plain arrays ;)
Also you can't annoy C people with that. -
@Joel-Bodenmann
You youngsters and your requirements ... plain arrays are plain, they need no stinkin' (custom) operators in the first place ... :DAlso you can't annoy C people with that.
With this I agree completely, but they can still annoy you by naming their variables
new
, like this:char new[5]; //< This always makes my day
-
@kshegunov
What about#define float int
? :p
If you hide stuff like this:#define if(x)
you can truly fuck someone's day.However, the most evil one might be this:
#define struct union
. -
-
@the_ said:
public class if { }
Ouch! There's tons of weird stuff a C dev might do, like:
struct class object;
Aha! We are bringing the heavy artillery, aren't we?
Since the preprocessor is not part of the language, and it's run before anything else ... and it's just string substitution, you can make very ugly stuff, like:
#define true 0 #define false 1
-
Aha! We are bringing the heavy artillery, aren't we?
No Sir, this is the heavy artillery:
#define if(x) if((__LINE__ % > 500)^(x))
Everything works well as long as the source doesn't grow larger than 500 lines.
And if you want to keep people from fucking with your sources/algorithms, you can carefully write a program with this:
#define if(x) if((__LINE__ % 5==0)^(x))
If done right, your stuff will work fine for as long as nobody adds or removes a line.
</topic>
-
This is (somewhat) tractable. My greatest clash with the preprocessor was when I was served with a "class" that defined a 256 item color table by means of members for each of 256 colors ... the properties, getters and setters were naturally defined through preprocessor macros. It was one of the low points of my life ... :)
-
@kshegunov
You might like my embedded graphics/GUI library then. Everything that can be done with preprocessor macros is done using preprocessor macros:https://bitbucket.org/Tectu/ugfx/src/fb100bcc25225cfad0bab5d40bef1db703e9670a/src/gdisp/gdisp_colors.h?at=master&fileviewer=file-view-default
https://bitbucket.org/Tectu/ugfx/src/fb100bcc25225cfad0bab5d40bef1db703e9670a/src/gdisp/gdisp_driver.h?at=master&fileviewer=file-view-default#gdisp_driver.h-812If you keep browsing the sources you will find other goodies :)
-
@Joel-Bodenmann
Right ... because my life doesn't suck enough and I would like to suffer more? I'm with @Wieland on that preprocessor magic - I avoid as the devil avoids incense ... there's a whole battery of cheat-sheets for dealing with that ugly monster ... -
@kshegunov
If your target processor runs on 64 MHz and has 32 kB RAM and you want to do some graphics/GUI on it, you might be thankful that there are masochistic people out there :)Of course I'd never do that on a desktop system.
Actually, the real problem I see with preprocessor macros is not actually writing and maintaining them but debugging them.
-
@Joel-Bodenmann
One of the reasons I stick to real CPUs. ;)
Joke aside, I leave that not-enough ram/CPU time struggle to engineers/embedded devs, I don't have the stomach for it to be honest. :DAs a side note I'm not completely convinced using
#define
for constants is really warranted.
If you're doing a comparison for example, this would (if I remember correctly) expand to something along the lines of:mov eax, 0x... # Set the constant test eax, [0x...] # Do the comparison with a field from memory
If you use a simple constant (const static variable), the above should be pretty much the same:
mov eax, [0x...] # Set the constant (from memory this time) test eax, [0x...] # Do the comparison with a field from memory