Home > Refactorings > Refactoring C++ macros using CodeRush

Refactoring C++ macros using CodeRush

September 17th, 2012

CodeRush Pro includes three refactorings that work with macros, aliases and typedef declarations in C++. Here they are:

Inline Macro

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced with the contents of the macro by the preprocessor when an application is being compiled. To replace the current macro with its definition in the source code, the Inline Macro refactoring can be used. If the current macro contains other nested macros in its definition, all macros are inlined:

CodeRush Inline Macro Preview

The refactoring available for all types of macros: object-like macros that resemble data objects when used, and function-like macros that resemble function calls.

Use Const

The Use Const refactoring converts an object-like macro that defines a simple identifier with a value into a const declaration:

CodeRush Use Const Preview

A constant declaration is useful for parameters which are used in the application but are do not need to be modified after the application is compiled. It has an advantage over the macros in that it is understood and used by the compiler itself. In other words, it is not substituted into the source code by the preprocessor before reaching the compiler as a macro definition, so error messages might be more helpful when using const declarations.

Use typedef

The Use typedef refactoring converts an object-like macro that defines a built-in data type or an existing class that is either provided by one f the libraries or defined in the code into a typedef declaration:

CodeRush Use typedef Preview

You can use typedef declarations to construct shorter or more meaningful names for types already defined by the language or for types that you have declared, e.g.:

CodeRush typedef Sample

Typedef names allow you to encapsulate implementation details that may change and make your code easier to modify, and more understandable.

Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Sep/17/2012
ID: R068

Similar Posts: