|
|
HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 2 The HP C++ PreprocessorMacro Replacement |
|
You can define C++ macros to substitute text in your source file.
A #define preprocessing directive of the form:
defines the identifier as a macro name that represents the replacement-list. The macro name is then replaced by the list of tokens wherever it appears in the source file (except inside of a string, character constant, or comment). A macro definition remains in force until it is undefined through the use of the #undef directive or until the end of the compilation unit.
Macros can be redefined without an intervening #undef directive. Any parameter used must agree in number and spelling with the original definition, and the replacement lists must be identical. All white space within the replacement-list is treated as a single blank space regardless of the number of white-space characters you use. For example, the following #define directives are equivalent:
The replacement-list may be empty. If the token list is not provided, the macro name is replaced with no characters. You can create macros that have parameters. The syntax of the #define directive that includes formal parameters is as follows:
The macro name is the identifier. The formal parameters are provided by the identifier-list enclosed in parentheses. The open parenthesis must immediately follow the identifier with no intervening white space. If there is a space between the identifier and the parenthesis, the macro is defined as if it were the first form and the replacement-list begins with the "(" character. The formal parameters to the macro are separated with commas. They may or may not appear in the replacement-list. When the macro is invoked, the actual arguments are placed in a parenthesized list following the macro name. Commas enclosed in additional matching pairs of parentheses do not separate arguments but are themselves components of arguments. The actual arguments replace the formal parameters in the token string when the macro is invoked. If a formal parameter in the macro definition directive's replacement string is preceded by a # operator, it is replaced by the corresponding argument from the macro invocation, preceded and followed by a double-quote character (") to create a string literal. This feature, available only with the ANSI C preprocessor, may be used to turn macro arguments into strings. This feature is often used with the fact that HP C++ concatenates adjacent strings. For example,
After HP C++ expands the macro definition in the preceding program, the following code results:
Use the special ## operator to form other tokens by concatenating tokens used as actual arguments. Each instance of the ## operator is deleted and the tokens preceding and following the ## are concatenated into a single token. If either of these names is a formal parameter of the macro, the corresponding argument at invocation is used. This is useful in forming unique variable names within macros. The following illustrates the ## operator:
Preprocessing the preceding program yields the following:
You can use the # and ## operators together:
Preprocessing this example yields the following code for the main procedure:
After compiling the code with CC and running the resulting executable file, you get the following results:
Spaces around the # and ## are optional.
In both the # and ## operations, the arguments are substituted as is, without any intermediate expansion. After these operations are completed, the entire replacement text is re-scanned for further macro expansions. The most common use of the macro replacement is in defining a constant. In C++ you can also declare constants using the keyword const. See "Constants" in Chapter 1 “Overview of HP C++” for more information. Rather than explicitly putting constant values in a program, you can name the constants using macros, then use the names in place of the constants. By changing the definition of the macro, you can more easily change the program:
In this example, the array x is dimensioned using the macro ARRAY_SIZE rather than the constant 1000. Note that expressions that may use the array can also use the macro instead of the actual constant:
Changing the dimension of x means only changing the macro for ARRAY_SIZE. The dimension changes and so do all of the expressions that make use of the dimension. Two other macros include:
The following macro is more complex. It has two parameters and produces an inline expression which is equal to the maximum of its two parameters:
Following are additional macro examples.
In C++ you can use named constants and inline functions to achieve results similar to using macros. You can use const variables in place of macros. Refer to "Constant Data Types" in Chapter 1 “Overview of HP C++”, "Overview of HP C++," for details. You can also use inline functions in many C++ programs where you would have used a function-like macro in a C program. Using inline functions reduces the likelihood of unintended side effects, since they have return types and generate their own temporary variables where necessary. The following program illustrates the replacement of a macro with an inline function:
In addition to __LINE__ and __FILE__ (refer to "Line Control" below), HP C++ provides the __DATE__, __TIME__, __STDCPP__, __cplusplus and c_plusplus| predefined macros. Table 2-1 “Predefined Macros” describes the complete set of macros that are predefined to produce special information. They may not be undefined. Table 2-1 Predefined Macros
|
|