Wow, that's a hideous abuse of the preprocessor.
deceptikon
Challenge Accepted
3,426 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
So I figured I'd post it for others, even though a lot of the programmers probably already have this list.
Well, I guess I didn't have that list... I think that I will now add it to my list of horrible things that YOU SHOULD NOT DO with MACROs.
mike_2000_17
21st Century Viking
3,135 posts since Jul 2010
Reputation Points: 2,050
Solved Threads: 625
Skill Endorsements: 41
In this article we try to abuse the preprocessor so much that C++ no longer looks like C++.
#include <iostream>
#define also ,
#define stop ;
#define begin {
#define end }
#define args(x) (x)
#define scalar int
#define letter char
#define ref(x) x*
#define seq(x, n) x[n]
#define start main
#define print std::cout <<
#define nl << std::endl
#define from(x) = x;
#define to(x) i < x; i++)
#define repeat_with(x) for (int x
#define index(a, i) a[i]
scalar start args(scalar count also ref(letter) seq(args,))
begin
repeat_with(i) from(0) to(count)
begin
print index(args, i) nl stop
end
end
deceptikon
Challenge Accepted
3,426 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
I find that nobody really uses preprocessor properly (aside from include statements of course). In fact they can almost always be removed completely since most IDEs have solid search/replace functionality (which is basically all that the define statement is). It is a lot faster to notice that you are typing a few things repeatedly, plug in an identifier for it (I usually use invalid C++ syntax) then find and replace. Here is an example for a char by char hello world program:
source:
int main()
{
//~1~~2~//copy and paste this 12 times!
~1~h~2~
~1~e~2~
~1~l~2~
~1~l~2~
~1~o~2~
~1~ ~2~
~1~w~2~
~1~o~2~
~1~r~2~
~1~l~2~
~1~d~2~
return 0;
}
Then you just hit CTRL-R (typical short-cut for replace) and type in replace ~1~ with cout<<' and replace ~2~ with '; These are even more versatile in many ways than a preprocessor because they have no ties to syntax at all!
I do notice that find-replace lacks that functional ability of macros however... but MS Word has a sick find and replace (it can be function and self modifying!) it should not be too long before such search capabilies are made standard on most IDEs.
Labdabeta
Practically a Master Poster
613 posts since Feb 2011
Reputation Points: 27
Solved Threads: 32
Skill Endorsements: 1
In gereral it's a really bad idea to use macros with very short names, like n. It removes the posibility of ever using that symbol as a variable name in your code. I think it's generally a good idea to try and use longish upper-case names for macros.
Additionally, if you're not careful (as Deceptikon showed) it's really easy to make your code pretty much unreadable using too many macros. Making a macro to replace std::cin << is largely pointless, it doesn't really save you that much, but adds loads of confusion for anyone trying to (literally) decypher your code.
On the point of the loop macro. Obviously, this is a pointless, terrible implementation of this idea. However, if you want a reasonably good macro for doing this, then you could use BOOST_FOREACH to do a much better job.
#define can be useful for doing OS/architecture/configuration dependent stuff. For instance, VC++ requires exporting of DLLs if you want to use them in a different dll, but gcc does this by default:
#ifdef __GNUC__
#define DLL_EXPORT
#else
#define DLL_EXPORT __declspec( dllexport )
#endif
Then you can define functions that you want exporting in VC++ as
DLL_EXPORT void myFunc();
Obviously, you'd need much better compiler detection than shown here, but it's just an example :o)
ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 8