]>
Commit | Line | Data |
---|---|---|
c63539ff ML |
1 | .. |
2 | Copyright 1988-2022 Free Software Foundation, Inc. | |
3 | This is part of the GCC manual. | |
4 | For copying conditions, see the copyright.rst file. | |
5 | ||
6 | .. index:: undefining macros, redefining macros, #undef | |
7 | ||
8 | .. _undefining-and-redefining-macros: | |
9 | ||
10 | Undefining and Redefining Macros | |
11 | ******************************** | |
12 | ||
13 | If a macro ceases to be useful, it may be :dfn:`undefined` with the | |
14 | :samp:`#undef` directive. :samp:`#undef` takes a single argument, the | |
15 | name of the macro to undefine. You use the bare macro name, even if the | |
16 | macro is function-like. It is an error if anything appears on the line | |
17 | after the macro name. :samp:`#undef` has no effect if the name is not a | |
18 | macro. | |
19 | ||
20 | .. code-block:: | |
21 | ||
22 | #define FOO 4 | |
23 | x = FOO; → x = 4; | |
24 | #undef FOO | |
25 | x = FOO; → x = FOO; | |
26 | ||
27 | Once a macro has been undefined, that identifier may be :dfn:`redefined` | |
28 | as a macro by a subsequent :samp:`#define` directive. The new definition | |
29 | need not have any resemblance to the old definition. | |
30 | ||
31 | However, if an identifier which is currently a macro is redefined, then | |
32 | the new definition must be :dfn:`effectively the same` as the old one. | |
33 | Two macro definitions are effectively the same if: | |
34 | ||
35 | * Both are the same type of macro (object- or function-like). | |
36 | ||
37 | * All the tokens of the replacement list are the same. | |
38 | ||
39 | * If there are any parameters, they are the same. | |
40 | ||
41 | * Whitespace appears in the same places in both. It need not be | |
42 | exactly the same amount of whitespace, though. Remember that comments | |
43 | count as whitespace. | |
44 | ||
45 | These definitions are effectively the same: | |
46 | ||
47 | .. code-block:: c++ | |
48 | ||
49 | #define FOUR (2 + 2) | |
50 | #define FOUR (2 + 2) | |
51 | #define FOUR (2 /* two */ + 2) | |
52 | ||
53 | but these are not: | |
54 | ||
55 | .. code-block:: c++ | |
56 | ||
57 | #define FOUR (2 + 2) | |
58 | #define FOUR ( 2+2 ) | |
59 | #define FOUR (2 * 2) | |
60 | #define FOUR(score,and,seven,years,ago) (2 + 2) | |
61 | ||
62 | If a macro is redefined with a definition that is not effectively the | |
63 | same as the old one, the preprocessor issues a warning and changes the | |
64 | macro to use the new definition. If the new definition is effectively | |
65 | the same, the redefinition is silently ignored. This allows, for | |
66 | instance, two different headers to define a common macro. The | |
3ed1b4ce | 67 | preprocessor will only complain if the definitions do not match. |