answersLogoWhite

0

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point.

Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself.

Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

What is inline function what are the advantages of using inline function?

Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say: int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro: #define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like: max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like: max(a++, b++) An alternative in C++ is to use inline functions: inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type. Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say: int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro: #define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like: max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like: max(a++, b++) An alternative in C++ is to use inline functions: inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type.


What is the difference between mirco and macro?

macro-The climate of a large geographic area. micro- is a local atmospheric zone where the climate differs from the surrounding area


What is the difference between Fanuc CNc and Siemens CNC?

Can't beat the Siemens!


What is main difference between macro and subroutins?

marco expand where it invoked ,subroutine will go where the subroutine is defined....


What is a macro in c plus plus?

A macro is preprocessor definition that is processed prior to compilation. All occurances of a macro within your C++ source code are replaced with the macro definition, much like an automated search-and-replace-all operation. If the macro's definition is a function with one or more arguments, then the function is inline expanded within your code, replacing the defined arguments with the arguments that were passed to the macro. However, macro functions are not type safe and cannot be debugged because they only exist in your source code; the compiler only sees the intermediate code emitted by the preprocessor, at which point all macro definitions will no longer exist. To address this problem, C++ also supports the concept of template functions, which not only eliminates any unwanted inline expansion (resulting in smaller code), but also ensures that all calls to the function are type safe and can be debugged in the normal way. That said, macro functions, when used appropriately, can greatly simplify your code and can achieve things that would be difficult if not impossible to achieve with C++ alone. The ability to use code fragments via a macro is one such possibility. However, when combined with preprocessor directives such as #ifdef DEBUG, macros can also be used to provide useful and powerful debugging routines that only exist in debug code, compiling to no code in release builds. This cannot be achieved with C++ alone.

Related Questions

Difference between macro and micro?

Macro is big micro is little


What is the difference between executing a macro and calling a macro?

Calling a macro loads the macro into memory, while executing the macro runs the macro.


What is the difference between macro and micro neutrons?

macro is bigger than micro


Give the differences between micro and macro economics?

ten difference of micro economics macro economics


What is the difference between a macro burst and a micro burst?

nuguu


Difference between micro hardness and macro hardness test?

micro is on a small scale and macro on a larger scale


What is the difference between macro and microeconomics?

macro- and microeconomics courses (the "big picture" versus individual companies/persons)


What is the difference between Marco and aperture?

The same as the difference between huoses and oranges. Did you mean "macro" instead of "marco"?


Differences between micro environment and macro environment of an organization?

Uranus is the greenish blue planet and it is gaseous.


In embedded systems perspective which is better to use. Macro or function?

In VBA, a macro is the name for the function that you can see on the GUI and use, and a function is a procedure called by a macro. A Macro is more "on the stage", and a Function is more "backstage".


What is the Major difference between micro and macro ecomics according to McConnell?

Micro


What is inline function what are the advantages of using inline function?

Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say: int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro: #define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like: max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like: max(a++, b++) An alternative in C++ is to use inline functions: inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type. Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say: int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro: #define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like: max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like: max(a++, b++) An alternative in C++ is to use inline functions: inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type.