Replacing text macros
De cppreference.com
< cpp | preprocessor
[] |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
Le prprocesseur prend en charge le remplacement macro de texte. Fonction macro-like remplacement de texte est galement pris en charge .
Original:
The preprocessor supports text macro replacement. Function-like text macro replacement is also supported.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Syntaxe
#define identifier replacement-list
|
(1) | ||||||||
#define identifier( parameters ) replacement-list
|
(2) | ||||||||
#define identifier( parameters, ... ) replacement-list
|
(3) | ||||||||
#define identifier( ... ) replacement-list
|
(4) | ||||||||
#undef identifier
|
(5) | ||||||||
Explication
directives
Les directives
#define dfinir la identifier comme macro, c'est indiquer au compilateur de remplacer toutes les occurrences successives de identifier avec replacement-list, qui peuvent tre ventuellement transforms en outre. Si l'identificateur est dj dfinie comme tout type de macro, le programme est mal form . Original:
The
#define directives define the identifier as macro, that is instruct the compiler to replace all successive occurrences of identifier with replacement-list, which can be optionally additionally processed. If the identifier is already defined as any type of macro, the program is ill-formed. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Objet de type macros
Objet de type macros remplacer toutes les occurrences de identifier dfini avec replacement-list. Version (1) de la directive
#define se comporte exactement comme a .Original:
Object-like macros replace every occurrence of defined identifier with replacement-list. Version (1) of the
#define directive behaves exactly like that.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Fonction de type macros
Fonction de type macros remplacer chaque occurrence de identifier dfini avec replacement-list, en plus de prendre un certain nombre d'arguments, qui a ensuite remplacer les occurrences correspondantes de toute la parameters dans le replacement-list. Le nombre d'arguments doit tre le mme que le nombre d'arguments dans la dfinition macro (parameters) ou le programme est mal form. Si l'identifiant n'est pas dans la notation fonctionnelle, c'est dire ne pas avoir parenthses aprs lui-mme, il n'est pas remplac tous .
Original:
Function-like macros replace each occurrence of defined identifier with replacement-list, additionally taking a number of arguments, which then replace corresponding occurrences of any of the parameters in the replacement-list. The number of arguments must be the same as the number of arguments in macro definition (parameters) or the program is ill-formed. If the identifier is not in functional-notation, i.e. does not have parentheses after itself, it is not replaced at all.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Version (2) de la directive
#define dfinit une fonction simple de type macro .Original:
Version (2) of the
#define directive defines a simple function-like macro.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Version (3) de la directive
#define dfinit une macro de type fonction avec un nombre variable d'arguments. Les arguments complmentaires peuvent tre accds en utilisant identifiant __VA_ARGS__, qui est ensuite remplac par des arguments, fournis avec l'identifiant soit remplac .Original:
Version (3) of the
#define directive defines a function-like macro with variable number of arguments. The additional arguments can be accessed using __VA_ARGS__ identifier, which is then replaced with arguments, supplied with the identifier to be replaced.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Version (4) de la directive
#define dfinit une macro de type fonction avec un nombre variable d'arguments, mais aucun argument rguliers. Les arguments peuvent tre accds seulement avec identifiant __VA_ARGS__, qui est ensuite remplac par des arguments, fournis avec identifiant doit tre remplac .Original:
Version (4) of the
#define directive defines a function-like macro with variable number of arguments, but no regular arguments. The arguments can be accessed only with __VA_ARGS__ identifier, which is then replaced with arguments, supplied with identifier to be replaced.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
et oprateurs
En fonction de type macros, un oprateur
# avant un identificateur dans le replacement-list fonctionne l'identifiant grce au remplacement des paramtres et enferme le rsultat entre guillemets, crant ainsi une chane littrale. En outre, le prprocesseur ajoute backslahes d'chapper les guillemets entourant les chanes littrales intgres, le cas chant, et double les anti-slash dans la chane si ncessaire. Tous les espaces initiaux et finaux est enlev, et toute squence d'espaces dans le milieu du texte (mais pas les littraux de chane incorpors l'intrieur) est rduit un seul espace. Cette opration est appele "La mise en chane". Si le rsultat de La mise en chane n'est pas une chane valide littral, le comportement est indfini .Original:
In function-like macros, a
# operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal. In addition, the preprocessor adds backslahes to escape the quotes surrounding embedded string literals, if any, and doubles the backslashes within the string as necessary. All leading and trailing whitespace is removed, and any sequence of whitespace in the middle of the text (but not inside embedded string literals) is collapsed to a single space. This operation is called "stringification". If the result of stringification is not a valid string literal, the behavior is undefined.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Un oprateur
## entre deux identificateurs successifs dans le replacement-list court remplacement de paramtres sur les deux identifiants, puis concatne le rsultat. Cette opration est appele concatnation ou jeton coller". Seuls les jetons qui forment ensemble un jeton valide peut tre coll: identificateurs qui forment un identifiant plus, des chiffres qui forment un certain nombre d'oprateurs, ou + et = qui forment un +=. Un commentaire ne peut pas tre cr par / coller et * parce que les commentaires sont supprims partir du texte avant la substitution macro est pris en compte. Si le rsultat de la concatnation n'est pas un jeton valide, le comportement est indfini .Original:
A
## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by pasting / and * because comments are removed from text before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
directive
La directive
#undef annule la dfinition du identifier, qui annule la dfinition prcdente du identifier par la directive #define. Si l'identificateur de ne pas avoir associ macro, la directive est ignore .Original:
The
#undef directive undefines the identifier, that is cancels previous definition of the identifier by #define directive. If the identifier does not have associated macro, the directive is ignored.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Macros prdfinies
Les noms des macros suivantes sont prdfinies dans une unit de traduction .
Original:
The following macro names are predefined in any translation unit.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
__cplusplus |
dveloppe pour 199711L (avant C++11) valeur ou 201103L (depuis C++11) Original: expands to value 199711L (avant C++11) or 201103L (depuis C++11) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__STDC_HOSTED__ (C++11) |
s'tend l' 1 entier constant si la mise en uvre est hberg (fonctionne sous un systme d'exploitation), si 0 autonome (fonctionne sans systme d'exploitation) Original: expands to the integer constant 1 if the implementation is hosted (runs under an OS), 0 if freestanding (runs without an OS) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__FILE__ |
pour le nom du fichier en cours, comme une chane de caractres littrale Original: expands to the name of the current file, as a character string literal The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__LINE__ |
dilate pour le numro de ligne du fichier source, une constante entire Original: expands to the source file line number, an integer constant The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__DATE__ |
dveloppe la date de la traduction, une chane de caractres littrale de la forme "mmm jj aaaa". Le nom du mois comme c'est le cas gnr par std::asctime()Original: expands to the date of translation, a character string literal of the form "Mmm dd yyyy". The name of the month is as if generated by std::asctime()The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__TIME__ |
s'tend au moment de la traduction, une chane de caractres littrale de la forme "hh: mm: ss" Original: expands to the time of translation, a character string literal of the form "hh:mm:ss" The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
Les noms suivants macro supplmentaires peuvent tre prdfinis par les implmentations .
Original:
The following additional macro names may be predefined by the implementations.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
__STDC__ |
dfinie par l'implmentation de valeur, le cas chant Original: implementation-defined value, if present The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__STDC_VERSION__ (C++11) |
dfinie par l'implmentation de valeur, le cas chant Original: implementation-defined value, if present The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__STDC_ISO_10646__ (C++11) |
se dveloppe en une constante entire de la yyyymmL forme, si wchar_t utilise Unicode, la date indique la dernire rvision du Unicode pris en chargeOriginal: expands to an integer constant of the form yyyymmL, if wchar_t uses Unicode, the date indicates the latest revision of Unicode supportedThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__STDC_MB_MIGHT_NEQ_WC__ (C++11) |
dveloppe pour 1 si l'encodage de caractres larges du jeu de caractres de base peut ne pas correspondre leur codage troiteOriginal: expands to 1 if wide character encoding of the basic character set may not equal their narrow encodingThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__STDCPP_STRICT_POINTER_SAFETY__ (C++11) |
dveloppe pour 1 si la mise en uvre a std::pointer_safety stricteOriginal: expands to 1 if the implementation has strict std::pointer_safetyThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
__STDCPP_THREADS__ (C++11) |
dveloppe pour 1 si le programme peut avoir plus d'un thread d'excutionOriginal: expands to 1 if the program can have more than one thread of executionThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (constante macro) |
Les valeurs de ces macros (sauf pour
__FILE__ et __LINE__) reste constante tout au long de l'unit de traduction. Les tentatives visant redfinir ou annuler la dfinition de ces macros rsultat en un comportement indfini .Original:
The values of these macros (except for
__FILE__ and __LINE__) remain constant throughout the translation unit. Attempts to redefine or undefine these macros result in undefined behavior.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Remarque: dans le cadre de tous les corps de la fonction, il ya une fonction spciale variable locale prdfinie nomme
__func__ (depuis C++11), dfini comme un tableau de caractres statique contenant le nom de la fonction dans la mise en uvre dfini par format. Ce n'est pas une macro du prprocesseur, mais il est utilis conjointement avec __FILE__ et __LINE__, par exemple, par assert .Original:
Note: in the scope of every function body, there is a special function-local predefined variable named
__func__ (depuis C++11), defined as a static character array holding the name of the function in implementation-defined format. It is not a preprocessor macro, but it is used together with __FILE__ and __LINE__, e.g. by assert.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Exemple
#include <iostream>
//make function factory and use it
#define FUNCTION(name, a) int fun_##name() { return a;}
FUNCTION(abcd, 12);
FUNCTION(fff, 2);
FUNCTION(kkk, 23);
#undef FUNCTION
#define FUNCTION 34
#define OUTPUT(a) std::cout << #a << '\n'
int main()
{
std::cout << "abcd: " << fun_abcd() << '\n';
std::cout << "fff: " << fun_fff() << '\n';
std::cout << "kkk: " << fun_kkk() << '\n';
std::cout << FUNCTION << '\n';
OUTPUT(million); //note the lack of quotes
}
Rsultat :
abcd: 12
fff: 2
kkk: 23
34
million