Open In App

Importance of macros in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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




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


On the other hand,

C++ can use macros

for shorter names.

CPP




#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

.



Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads