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.
On the other hand, C++ can use macros for shorter names.
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.
- 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!
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Multitape Nondeterministic Turing Machine simulator
- LOB Rules and Restrictions
- Check whether Quadrilateral is valid or not if angles are given
- ML | Training Image Classifier using Tensorflow Object Detection API
- TensorFlow 2.0
- Data Structures and Algorithms Online Courses : Free and Paid
- Find the longest string that can be made up of other strings from the array
- Count substrings that contain all vowels | SET 2
- Flatten Binary Tree in order of Zig Zag traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.