Importance of macros in C++

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

Java.util.something.member(.....

chevron_right


On the other hand, C++ can use macros for shorter names.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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

chevron_right


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.



My Personal Notes arrow_drop_up

My name is Javene McGowan I live in Jamaica I have always been recognized as a bright boy and pride my self in learning All aspects of life At age 20 or 21 I became interested in Chess About a year after my attention drew to computer programming because of the mystique behind it How did those softwa

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.