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)
- Output of the Program | Use Macros Carefully!
- Interesting Facts about Macros and Preprocessors in C
- What is the Role of Java in the IT Industry?
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Implementing ANDNOT Gate using Adaline Network
- Program for Gauss Siedel Method (Computational Mathematics)
- AI Model For Neurodegenerative Diseases
- Area of a Regular Pentagram
- std::uniform_int_distribution class in C++
- C++ Boost String Algorithms Library
- Remove duplicates from an unsorted array using STL in C++
- boost::algorithm::is_sorted() in C++ library
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.