{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 10 traditional mode 50 cpp replaces an

Info icon This preview shows pages 54–56. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10: Traditional Mode 50 CPP replaces an unquoted object-like macro name with its replacement text, and then rescans it for further macros to replace. Unlike standard macro expansion, traditional macro expansion has no provision to prevent recursion. If an object-like macro appears unquoted in its replacement text, it will be replaced again during the rescan pass, and so on ad infinitum . GCC detects when it is expanding recursive macros, emits an error message, and continues after the offending macro invocation. #define PLUS + #define INC(x) PLUS+x INC(foo); 7→ ++foo; Function-like macros are similar in form but quite different in behavior to their ISO counterparts. Their arguments are contained within parentheses, are comma-separated, and can cross physical lines. Commas within nested parentheses are not treated as argument separators. Similarly, a quote in an argument cannot be left unclosed; a following comma or parenthesis that comes before the closing quote is treated like any other character. There is no facility for handling variadic macros. This implementation removes all comments from macro arguments, unless the ‘ -C ’ option is given. The form of all other horizontal whitespace in arguments is preserved, including leading and trailing whitespace. In particular f( ) is treated as an invocation of the macro ‘ f ’ with a single argument consisting of a single space. If you want to invoke a function-like macro that takes no arguments, you must not leave any whitespace between the parentheses. If a macro argument crosses a new line, the new line is replaced with a space when forming the argument. If the previous line contained an unterminated quote, the following line inherits the quoted state. Traditional preprocessors replace parameters in the replacement text with their argu- ments regardless of whether the parameters are within quotes or not. This provides a way to stringize arguments. For example #define str(x) "x" str(/* A comment */some text ) 7→ "some text " Note that the comment is removed, but that the trailing space is preserved. Here is an example of using a comment to effect token pasting. #define suffix(x) foo_/**/x suffix(bar) 7→ foo_bar 10.3 Traditional miscellany Here are some things to be aware of when using the traditional preprocessor. Preprocessing directives are recognized only when their leading ‘ # ’ appears in the first column. There can be no whitespace between the beginning of the line and the ‘ # ’, but whitespace can follow the ‘ # ’. A true traditional C preprocessor does not recognize ‘ #error ’ or ‘ #pragma ’, and may not recognize ‘ #elif ’. CPP supports all the directives in traditional mode that it
Image of page 54

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 11: Implementation Details 51 supports in ISO mode, including extensions, with the exception that the effects of #pragma GCC poison ’ are undefined.
Image of page 55
Image of page 56
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern