|
BDE 4.39.x Production Release
|
Provide a macro to increment preprocessor numbers.
Provide a macro to increment preprocessor numbers.
NUMBER + 1This component provides a macro, BSLS_MACROINCREMENT(NUMBER), that produces the preprocessor number equal to the number succeeding the supplied argument. This is necessary as the preprocessor performs arithmetic only inside the context of a #if directive. Common use cases for this macro would be #line directives or any attempt to provide a computed value when token-pasting to produce an identifier.
This section provides a brief description of the macro defined in this component.
Here we illustrate the typical use case, which demonstrates using the BSLS_MACROINCREMENT macro to change the effective name of a file without changing the effective line count.
First, we print out current file name and line number:
Now, we provide a new effective file name to compiler by supplying the new file name to #line directive:
Notice that the first argument following #line directive is the designated line number of the next line, so we increment current __LINE__ by calling BSLS_MACROINCREMENT to keep consistent line numbers.
Finally, print out current file name and line number again. Observe that the file name is changed to the one we set in previous #line directive while the line number still works as expected:
| #define BSLS_MACROINCREMENT | ( | MACRO_VALUE | ) | BSLS_MACROINCREMENT_IMPL(MACRO_VALUE) |
Return the preprocessor number that is the successor to MACRO_VALUE.
MACRO_VALUE is an integer literal, or a macro that ultimately expands to an integer literal. | #define BSLS_MACROINCREMENT_IMPL | ( | MACRO_VALUE | ) | BSLS_MACROINCREMENTED_VALUE##MACRO_VALUE |
The BSLS_MACROINCREMENT_IMPL macro is an implementation detail that forces the token-pasting to occur in a separate round of macro expansion, so that the computed macro will, in turn, expand to the desired literal.
| #define BSLS_MACROINCREMENTED_VALUE0 1 |
| #define BSLS_MACROINCREMENTED_VALUE1 2 |
| #define BSLS_MACROINCREMENTED_VALUE19999 20000 |
| #define BSLS_MACROINCREMENTED_VALUE2 3 |