99.9% of the C++ programs use macros. Unless you are making a basic file you have to write #include, which is a macro that pastes the text contained in a file. And it matters not the extension of the file. Macros are very powerful and can do things that not even templates, lambdas, constexpr, inlining or whatever future compiler constructs will ever do.
The thing about the CPP compiler, and generally compilers, or the language, is that it is designed to restrict accidents. Such as using a type as another type, not setting object state before using object, not releasing memory, using a garbage value, in case of java – accessing out of bounds memory, etc etc. This is all fine and good, in fact errors get caught easier, but on the other hand, it restricts programmer from doing useful stuff.
Then language creators end up having to devise means of getting around these restrictions. For example, Java has their JNI library for connecting to C and C++ DLLs.
Problem is Java does not have macros. Furthermore Java is a very verbatim language.
Java.util.something.member(..... |
On the other hand, C++ can use macros for shorter names.
#define BIT64 (8) #define TYPE std::conditional<size of(int) == BIT64, int, short>::type #define TRUE (std::is_same<TYPE, int>::value) #define TRY_ENABLE std::enable_if<TRUE, TYPE>::type TRY_ENABLE func(){}; #undef // every definition |
These illustrate many uses of macros:
- It can turn a long declaration into a short declaration.
- With a name that gives the statement a meaning, its easier to understand the macro than the lengthy code behind it.
- It is straight to the point. Doesn’t need to be in a namespace or pollute the namespace with using declaration.
- Macros don’t clean up after themselves, but that’s good, a programmer can decide when the Macro needs to exist and clean up so that another file can use the macro comfortably. Compare that to CPP using directive that persist for ever.
- Also since macros originated from C, what you see is what you get. What you C. Get it! The ++ in C++ means C in doubles. With things like ADL, default created constructors, they are a lot of ways for CPP to knock you out. Macros on the other hand are very simple, yet very powerful.
- Also notice how number 8 got turned into an identified name. The CPP alternative is const variables, constexpr functions. Probably constexpr provides the same results with no runtime over head. But declaring a const variable just to make code more readable seems as waste of memory. Why allocate memory just to give a name to a value? Then when you pass that variable to a parameter you might incur another memory allocation cost. With the Macro only one allocation cost is made. That is passing the macro value to the parameter.
So, macros not only make names shorter, and infact typedefs and ref alias can also be used to make names shorter, but macros can also avoid runtime overheads. Macros happen way before runtime. Macros have been avoiding run time over heads way before CPP features such as move and template.
Recommended Posts:
- Macros vs Functions
- Output of C++ programs | Set 25 (Macros)
- Interesting Facts about Macros and Preprocessors in C
- Output of the Program | Use Macros Carefully!
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- JSTL | JSP Standard Tag Library
- Top 3 tips an interviewee must have in mind
- Largest sub-set possible for an array satisfying the given condition
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only
- Program to print Step Pattern
- First and Last Three Bits
- Program to delete Nth digit of a Number
- Maximum number of 3-person teams formed from two groups
- Minimum elements to be added so that two matrices can be multiplied
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



