I came across a git repo in which a person did something interesting with some preprocessor defines. Rather than using it to define constants, they used it to actually create their own interface to C/C++. They did it more as a joke since their repo is actually a bit offensively named, but it was interesting. https://github.com/klange/assholedoth
I've decided to make my own version in which I make some shorthand instructions. (not for code that I intend to share with others)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// no #includes necessary, preprocessor doesn't care if
// functions/structs/types are defined beforehand...
// nor afterwords if you don't use the shorthand in the code.
#define pu public:
#define pr private:
// I know it's goofy, but I don't want to overwrite int main() if used
#define mane int main(int argc, char ** argv)
// Getting tired of typing std:: ?
#define str std::string
#define vec std::vector
#define o std::cout <<
So now I can call a vector of strings without "using namespace std" like this;
vec <str> names;
I'm not going to defend this code, I'm just trying to play a bit and it was fun.
I'm curious how you might abuse the preprocessor? Have you seen some interesting tricks?
For the most creative abuses, probably the best place to look is entries to IOCCC.
One time in a project I needed a little bit of reflection. I had a bunch of functions that I needed to expose to the outside and each had a related class that dealt with it. Basically I was going to need to do something with all the functions in a bunch of places, and always in the same order (or in an irrelevant order).
So I wrote a file that looked like this:
The upshot is, the openssl api functions cannot be found by searching the code, as the names are made up from macros. Most of the calls are arbitrary and there are few decent examples of how to use the api, coupled with breaking changes in almost all releases, makes openssl a true wonder of the computing world.