[RK_2014_1024][C++_02]The Predefined Macros

1. The C Preprocessor Macros

The C Preprocessor

1 Overview

1.1 Character sets

1.2 Initial processing

1.3 Tokenization

1.4 The preprocessing language

2 Header Files

2.1 Include Syntax

2.2 Include Operation

2.3 Search Path

2.4 Once-Only Headers

2.5 Alternatives to Wrapper #ifndef

2.6 Computed Includes

2.7 Wrapper Headers

2.8 System Headers

3 Macros

3.1 Object-like Macros

3.2 Function-like Macros

3.3 Macro Arguments

3.4 Stringification

3.5 Concatenation

3.6 Variadic Macros

3.7 Predefined Macros

3.7.1 Standard Predefined Macros

3.7.2 Common Predefined Macros

3.7.3 System-specific Predefined Macros

3.7.4 C++ Named Operators

3.8 Undefining and Redefining Macros

3.9 Directives Within Macro Arguments

3.10 Macro Pitfalls

3.10.1 Misnesting

3.10.2 Operator Precedence Problems

3.10.3 Swallowing the Semicolon

3.10.4 Duplication of Side Effects

3.10.5 Self-Referential Macros

3.10.6 Argument Prescan

3.10.7 Newlines in Arguments

4 Conditionals

4.1 Conditional Uses

4.2 Conditional Syntax

4.2.1 Ifdef

4.2.2 If

4.2.3 Defined

4.2.4 Else

4.2.5 Elif

4.3 Deleted Code

5 Diagnostics

6 Line Control

7 Pragmas

8 Other Directives

9 Preprocessor Output

10 Traditional Mode

10.1 Traditional lexical analysis

10.2 Traditional macros

10.3 Traditional miscellany

10.4 Traditional warnings

11 Implementation Details

11.1 Implementation-defined behavior

11.2 Implementation limits

11.3 Obsolete Features

11.3.1 Assertions

11.4 Differences from previous versions

12 Invocation

13 Environment Variables

GNU Free Documentation License

ADDENDUM: How to use this License for your documents

Index of Directives

Option Index

Concept Index
Table of Contents

 

2. Macros

3 Macros



A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.



You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords. This can be useful if you wish to hide a keyword such as const from an older compiler that does not understand it. However, the preprocessor operator defined (see Defined) can never be defined as a macro, and C++'s named operators (see C++ Named Operators) cannot be macros when you are compiling C++.



3.1 Object-like Macros

3.2 Function-like Macros

3.3 Macro Arguments

3.4 Stringification

3.5 Concatenation

3.6 Variadic Macros

3.7 Predefined Macros

3.8 Undefining and Redefining Macros

3.9 Directives Within Macro Arguments

3.10 Macro Pitfalls

 

3. Predefined Macros

3.7 Predefined Macros



Several object-like macros are predefined; you use them without supplying their definitions. They fall into three classes: standard, common, and system-specific.



In C++, there is a fourth category, the named operators. They act like predefined macros, but you cannot undefine them.



3.7.1 Standard Predefined Macros

3.7.2 Common Predefined Macros

3.7.3 System-specific Predefined Macros

3.7.4 C++ Named Operators

 

4. Standard Predefined Macros

3.7.1 Standard Predefined Macros



The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that implement those standards. Older compilers may not provide all of them. Their names all start with double underscores.



__FILE__

This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in ‘#include’ or as the input file name argument. For example, "/usr/local/include/myheader.h" is a possible expansion of this macro. 

__LINE__

This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, it's a pretty strange macro, since its “definition” changes with each new line of source code.

__FILE__ and __LINE__ are useful in generating an error message to report an inconsistency detected by the program; the message can state the source line at which the inconsistency was detected. For example,



     fprintf (stderr, "Internal error: "

                      "negative string length "

                      "%d at %s, line %d.",

              length, __FILE__, __LINE__);

An ‘#include’ directive changes the expansions of __FILE__ and __LINE__ to correspond to the included file. At the end of that file, when processing resumes on the input file that contained the ‘#include’ directive, the expansions of __FILE__ and __LINE__ revert to the values they had before the ‘#include’ (but __LINE__ is then incremented by one as processing moves to the line after the ‘#include’).



A ‘#line’ directive changes __LINE__, and may change __FILE__ as well. See Line Control.



C99 introduces __func__, and GCC has provided __FUNCTION__ for a long time. Both of these are strings containing the name of the current function (there are slight semantic differences; see the GCC manual). Neither of them is a macro; the preprocessor does not know the name of the current function. They tend to be useful in conjunction with __FILE__ and __LINE__, though.



__DATE__

This macro expands to a string constant that describes the date on which the preprocessor is being run. The string constant contains eleven characters and looks like "Feb 12 1996". If the day of the month is less than 10, it is padded with a space on the left.

If GCC cannot determine the current date, it will emit a warning message (once per compilation) and __DATE__ will expand to "??? ?? ????". 



__TIME__

This macro expands to a string constant that describes the time at which the preprocessor is being run. The string constant contains eight characters and looks like "23:59:01".

If GCC cannot determine the current time, it will emit a warning message (once per compilation) and __TIME__ will expand to "??:??:??". 



__STDC__

In normal operation, this macro expands to the constant 1, to signify that this compiler conforms to ISO Standard C. If GNU CPP is used with a compiler other than GCC, this is not necessarily true; however, the preprocessor always conforms to the standard unless the -traditional-cpp option is used.

This macro is not defined if the -traditional-cpp option is used.



On some hosts, the system compiler uses a different convention, where __STDC__ is normally 0, but is 1 if the user specifies strict conformance to the C Standard. CPP follows the host convention when processing system header files, but when processing user files __STDC__ is always 1. This has been reported to cause problems; for instance, some versions of Solaris provide X Windows headers that expect __STDC__ to be either undefined or 1. See Invocation. 



__STDC_VERSION__

This macro expands to the C Standard's version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version. This signifies which version of the C Standard the compiler conforms to. Like __STDC__, this is not necessarily accurate for the entire implementation, unless GNU CPP is being used with GCC.

The value 199409L signifies the 1989 C standard as amended in 1994, which is the current default; the value 199901L signifies the 1999 revision of the C standard. Support for the 1999 revision is not yet complete.



This macro is not defined if the -traditional-cpp option is used, nor when compiling C++ or Objective-C. 



__STDC_HOSTED__

This macro is defined, with value 1, if the compiler's target is a hosted environment. A hosted environment has the complete facilities of the standard C library available. 

__cplusplus

This macro is defined when the C++ compiler is in use. You can use __cplusplus to test whether a header is compiled by a C compiler or a C++ compiler. This macro is similar to __STDC_VERSION__, in that it expands to a version number. Depending on the language standard selected, the value of the macro is 199711L, as mandated by the 1998 C++ standard; 201103L, per the 2011 C++ standard; an unspecified value strictly larger than 201103L for the experimental languages enabled by -std=c++1y and -std=gnu++1y. 

__OBJC__

This macro is defined, with value 1, when the Objective-C compiler is in use. You can use __OBJC__ to test whether a header is compiled by a C compiler or an Objective-C compiler. 

__ASSEMBLER__

This macro is defined with value 1 when preprocessing assembly language.

 

5. Line Control

6 Line Control



The C preprocessor informs the C compiler of the location in your source code where each token came from. Presently, this is just the file name and line number. All the tokens resulting from macro expansion are reported as having appeared on the line of the source file where the outermost macro was used. We intend to be more accurate in the future.



If you write a program which generates source code, such as the bison parser generator, you may want to adjust the preprocessor's notion of the current file name and line number by hand. Parts of the output from bison are generated from scratch, other parts come from a standard parser file. The rest are copied verbatim from bison's input. You would like compiler error messages and symbolic debuggers to be able to refer to bison's input file.



bison or any such program can arrange this by writing ‘#line’ directives into the output file. ‘#line’ is a directive that specifies the original line number and source file name for subsequent input in the current preprocessor input file. ‘#line’ has three variants:



#line linenum

linenum is a non-negative decimal integer constant. It specifies the line number which should be reported for the following line of input. Subsequent lines are counted from linenum. 

#line linenum filename

linenum is the same as for the first form, and has the same effect. In addition, filename is a string constant. The following line and all subsequent lines are reported to come from the file it specifies, until something else happens to change that. filename is interpreted according to the normal rules for a string constant: backslash escapes are interpreted. This is different from ‘#include’.

Previous versions of CPP did not interpret escapes in#line’; we have changed it because the standard requires they be interpreted, and most other compilers do. 



#line anything else

anything else is checked for macro calls, which are expanded. The result should match one of the above two forms.

‘#line’ directives alter the results of the __FILE__ and __LINE__ predefined macros from that point on. See Standard Predefined Macros. They do not have any effect on ‘#include’'s idea of the directory containing the current file. This is a change from GCC 2.95. Previously, a file reading



     #line 1 "../src/gram.y"

     #include "gram.h"

would search for gram.h in ../src, then the -I chain; the directory containing the physical source file would not be searched. In GCC 3.0 and later, the ‘#include’ is not affected by the presence of a ‘#line’ referring to a different directory.



We made this change because the old behavior caused problems when generated source files were transported between machines. For instance, it is common practice to ship generated parsers with a source release, so that people building the distribution do not need to have yacc or Bison installed. These files frequently have ‘#line’ directives referring to the directory tree of the system where the distribution was created. If GCC tries to search for headers in those directories, the build is likely to fail.



The new behavior can cause failures too, if the generated file is not in the same directory as its source and it attempts to include a header which would be visible searching from the directory containing the source file. However, this problem is easily solved with an additional -I switch on the command line. The failures caused by the old semantics could sometimes be corrected only by editing the generated files, which is difficult and error-prone.

 

6. TestPredefinedMacros

#include <iostream>

using namespace std;



int

TestPredefinedMacros(void)

{

    cout << "-----" << endl;

    cout << "The name of the current function : \t" << __func__ << endl;

    cout << "The name of the current input file : \t" << __FILE__ << endl;

    cout << "The current input line number : \t" << __LINE__ << endl;

    cout << "The date on which the preprocessor is being run : " << __DATE__ << endl;

    cout << "The time on which the preprocessor is being run : " << __TIME__ << endl;

    cout << "-----" << endl;



    return 1;

}



int

main(void)

{

    cout << "-----" << endl;

    cout << "The name of the current function : \t" << __func__ << endl;

    cout << "The name of the current input file : \t" << __FILE__ << endl;

    cout << "The current input line number : \t" << __LINE__ << endl;

    cout << "The date on which the preprocessor is being run : " << __DATE__ << endl;

    cout << "The time on which the preprocessor is being run : " << __TIME__ << endl;

    cout << "-----" << endl;



    TestPredefinedMacros();



    return 0;

}
TestPredefinedMacros

 

7.本文网址[tom-and-jerry发布于2014-10-25 15:01]

http://www.cnblogs.com/tom-and-jerry/p/4050131.html

你可能感兴趣的:(C++)