This series of articles by the @yhl_leo produced, reproduced please indicate the source.
Article Link: http://blog.csdn.net/yhl_leo/article/details/48879093
1 #
##
function and usage of
The function of a C + + macro #
is to string the macro arguments that follow it, simply by substituting a double quote in the left and right of the macro variable that it references. The ##
connection symbol consists of two well numbers, and the function is to concatenate two of them in a macro definition with parameters to form a new substring. But it cannot be the first or last substring.
#include <iostream>using namespace STD;#define WARN_IF (exp) IF (exp) cerr << #EXP << Endl;#define Paster (n) cout << "token" << #n << "=" << n << Endl;#define _cons (A, b) int (a##+# #b)#define _STRI (s) #svoidMain () {intdiv =0; warn_if (div = =0);//Prints:div = = 0Paster (9);//prints:token9 = 9 cout<< _cons (1,2) << Endl;//Prints:3 cout<< _stri (Int_max) << Endl;//Prints:int_max}
#
##
Macro Parameters that are useful or in the macro definition are no longer expanded, for example, are _STRI(INT_MAX)
INT_MAX
not expanded to 2147483647. If you want to expand the macro parameters, you need to add a layer of intermediate conversion macros:
#define STRI(s) _STRI(s)cout// prints : 2147483647
The intention of adding this layer of macro is to put all the macro parameters in this layer to expand all, then the macro in the conversion macro can get the corresponding macro parameters.
Next, let's look at some of the uses for creating conditional compilation parameters with preprocessing directives to control code compilation.
#include
usage of 2
An operation that contains a header file, usually in two formats:
#include #include "header-file"
<>
And ""
indicates that the compiler is in a different order when searching for a header file:
<>
Indicates that the search is started from the system directory, and then the directory listed by the path environment variable is searched, and the current directory is not searched
""
is to start the search from the current directory and then the directory listed by the System directory and PATH environment variables.
Therefore, the system header files are generally used, the <>
user's own definition can be used ""
to speed up the search speed. In addition, write more code, you will find that some of the header file between the inclusion of a hidden dependency, it must be noted. The Google C + + Style Guide also emphasizes the use of standard header file inclusion sequences to enhance readability and avoid hiding dependencies:
1 Related documents (priority position, e.g. dir2/foo2.h
)
2 C System files
3 C + + system files
4 files for other libraries .h
5 documents in this project .h
3,, #if
#elif
#else
, #endif
usage
// structure 1#if constant_expression#else#endif// structure 2#if constant_expression#elif constant_expression#endif
The structure here is similar to the common ones if...else
if...else if...else
, when #if
the following conditions are not 0 (true), compile and #if
#else
or #elif
between the code, otherwise compile #else
and #endif
between the code (or Judge #elif
After the condition is not 0 (true), decide whether to compile #elif
and #endif
between the code).
#if 1 cout << "Hello world!" << endl; #else cout << " nice To meet you! " << Endl; #endif //prints:hello world!
#if 1 cout << "Hello world!" << endl; #elif 1 cout << nice To meet you! " << Endl; #endif //Prints:hello world! //Nice to meet you!
4,, #define
#undef
#ifdef
, #ifndef
usage
#define
is a common macro definition method, and the usage structure is:
// #define identifier replacement-code#define PI 3.1415926#define ADD(x,y) (x + y)
#undef
As the name implies, the previously defined macro is removed from that point , and if the identifier is not currently defined as a macro name, the directive is ignored:
// #undef identifier#undef PI
#ifdef
Contrary to the #ifndef
meaning, the former means that if the macro is defined, the code is compiled, and the corresponding code is compiled if the macro is not defined. The common structure is:
/* * #ifdef identifier * #else or #elif * #endif**/#define DEBUG#ifdef DEBUG cout"This is a debug message." << endl;#endif// prints : This is a debug message./* * #ifndef identifier * #else or #elif * #endif**/#ifndef DEBUG cout"This is a debug message." << endl;#endif// prints nothing
When programming, in order to avoid the header file redefinition, often use is the #define
match condition compiles solves:
#ifndef MY_HEADER_FILE_ H #define MY_HEADER_FILE_H //... class myheaderfile{//..... }; #endif //my_header_file_h
In addition to #pragma once
this, the use of this command at the very beginning of the header file will ensure that the header file is compiled once. (in all pre-processing directives, #pragma
directives are probably the most complex, and their role is to set the state of the compiler or to instruct the compiler to complete some specific actions, not much in this article.) )
5 #line
Usage
#line
Commands are values that are used for changes __LINE__
and __FILE__
variables. __FILE__
and __LINE__
describes the current file and the number of rows being read.
// #line line-number filenameint main(){#line 10 "main.cpp" cout" " << __LINE__ << endl;}// prints : main.cpp 10
6 #error
Usage
#error
Will directly cause the program to stop compiling and output the specified error message:
// #error message#ifndef VERSION#error Version number not specified.#endif// The compiler will halt compiling and return with the specified error message: // fatal error C1189: #error : Version number not specified.
The role and usage of C + + "#"