answersLogoWhite

0


Best Answer

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

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Preprocessor macros are a text substitution pattern that is applied throughout your code prior to compilation. Inline functions are actual functions that are inline expanded, replacing every call to those functions with inline code. But while macros can be used to simulate inline expansion, it is never recommended.

The compiler performs an enormous amount of analysis during pre-compilation in order to produce the most efficient code possible. While inline expansion will certainly produce a faster function (by eliminating the function call altogether) it is at the expanse of code size, which will reduce the overall performance of your code. However, the inline keyword is merely a hint to the compiler that a function is a candidate for inline expansion, nothing more -- it is free to ignore the request. Compilation flags give you a certain degree of control over how much expansion is actually applied, but ultimately the compiler decides what is best.

Macro substitution effectively bypasses the compiler and expands functions whether it is desirable or not. While you will certainly gain full control over expansions, macros are not type-safe. Substitution may result in contextual errors or unexpected side-effects. Static variables are also handled differently. And while compile-time errors are fairly easy to deal with, runtime errors within macros are more difficult to track down. Macros may also result in unexpected function overloads that would be far better handled with inline template functions, which naturally enlists the compiler's wealth of analysis.

Ultimately, the compiler is there to assist you, so it makes sense to enlist it as much as possible. If there is a choice between a macro and an inline function, go with the inline function every time.

It should be noted that recursive functions cannot be inline expanded nor macro substituted, for obvious reasons.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between macro and inline function in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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 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.


What is main difference between macro and subroutins?

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

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


What is the difference between macro and micro decomposers?

The difference between macro and micro decomposers is micro decomposers can't be seen by the naked eye while macro decomposers are big enough so they can be seen.


Micro to macro processes in an ecosystem?

t s the difference between micro and macro process in the ecosystem?


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"?


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".


Differences between micro environment and macro environment of an organization?

Uranus is the greenish blue planet and it is gaseous.