answersLogoWhite

0

What is a macro in c plus plus?

Updated: 8/10/2023
User Avatar

Wiki User

10y ago

Best Answer

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.

User Avatar

Wiki User

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

Wiki User

10y ago

A macro function is a macro definition that represents a typeless function definition. All occurrences of the macro symbol within the source code are expanded with the macro definition, replacing the parameters within the macro definition with the arguments or expressions supplied by the source code.

Since macro expansion is a precompiler operation, the compiler has no access to the original macro, and the intermediate source will contain expanded code that doesn't exist in the source. This means that any side-effects resulting from an incorrect usage of the macro cannot be identified within the source. In other words, the compiler may or may not be able to identify a problem resulting from macro expansion, but even if it can, it cannot tell you how to resolve it since it is compiling the expanded code, not the original source code.

The biggest problem with macro functions (and macros in general) is that they are typeless, while C++ is a strongly-typed language. Since the compiler cannot debug a macro, this can often result in untraceable side-effects. For instance, when passing an expression as an argument to a macro function, it is important that the macro function evaluate the parameter before processing it, otherwise the expression will be evaluated during processing, which typically results in multiple evaluations with undefined behaviour.

Another problem with macro functions is that they are always inline expanded, even in debug code. While inline expansion reduces the need to make an otherwise time consuming function call, the increased code size could easily have the opposite effect, slowing overall performance. The compiler's optimisers are in a much better position to decide if inline expansion is warranted.

Function templates are always the preferred option to macro functions, as they are type safe, can be easily debugged, and adhere to the compiler's inline expansion optimiser. Macro functions should only be utilised if they would result in cleaner code than would otherwise be possible with C++ alone (simplifying an otherwise complex C++ routine), or where C++ alone is incapable of achieving a desired effect. Macro functions are also useful when it comes to debugging source code, such us providing user-defined trace and assertion routines, as the macro function can be collapsed to no code in release builds far more easily than with C++ code. However, macro functions are more difficult to write and maintain, and impossible to debug. Keep in mind that macros and C++ are essentially completely separate languages, and that there is no interpreter. The compiler cannot help you so all responsibility for both defining and using macros correctly lies with you, the programmer.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Macro (in C language) is a way to map a certain input sequence to a sequence of character. The mapping is performed in the pre-processor stage.

Macro can take parameters.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a macro in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp