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. |
Le préprocesseur 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.
Sommaire |
[modifier] 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) | ||||||||
[modifier] Explication
[modifier] NJ directives
Les directives
#define
définir la identifier comme macro, c'est indiquer au compilateur de remplacer toutes les occurrences successives de identifier avec replacement-list, qui peuvent être éventuellement transformés en outre. Si l'identificateur est déjà définie 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.
[modifier] Objet de type macros
Objet de type macros remplacer toutes les occurrences de identifier défini 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.
[modifier] Fonction de type macros
Fonction de type macros remplacer chaque occurrence de identifier défini 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 même que le nombre d'arguments dans la définition macro (parameters) ou le programme est mal formé. Si l'identifiant n'est pas dans la notation fonctionnelle, c'est à dire ne pas avoir parenthèses après lui-même, 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
définit 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
définit une macro de type fonction avec un nombre variable d'arguments. Les arguments complémentaires peuvent être accédés 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
définit une macro de type fonction avec un nombre variable d'arguments, mais aucun argument réguliers. Les arguments peuvent être accédés 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.
[modifier] NJ et NJ opérateurs
En fonction de type macros, un opérateur
#
avant un identificateur dans le replacement-list fonctionne l'identifiant grâce au remplacement des paramètres et enferme le résultat entre guillemets, créant ainsi une chaîne littérale. En outre, le préprocesseur ajoute backslahes d'échapper les guillemets entourant les chaînes littérales intégrées, le cas échéant, et double les anti-slash dans la chaîne si nécessaire. Tous les espaces initiaux et finaux est enlevé, et toute séquence d'espaces dans le milieu du texte (mais pas les littéraux de chaîne incorporés à l'intérieur) est réduit à un seul espace. Cette opération est appelée "La mise en chaîne". Si le résultat de La mise en chaîne n'est pas une chaîne valide littéral, le comportement est indéfini .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 opérateur
##
entre deux identificateurs successifs dans le replacement-list court remplacement de paramètres sur les deux identifiants, puis concatène le résultat. Cette opération est appelée «concaténation» 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'opérateurs, ou +
et =
qui forment un +=
. Un commentaire ne peut pas être créé par /
coller et *
parce que les commentaires sont supprimés à partir du texte avant la substitution macro est pris en compte. Si le résultat de la concaténation n'est pas un jeton valide, le comportement est indéfini .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.
[modifier] NJ directive
La directive
#undef
annule la définition du identifier, qui annule la définition précédente du identifier par la directive #define
. Si l'identificateur de ne pas avoir associé macro, la directive est ignorée .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.
[modifier] Macros prédéfinies
Les noms des macros suivantes sont prédéfinies 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 |
développe 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 hébergé (fonctionne sous un système d'exploitation), si 0 autonome (fonctionne sans système 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 chaîne de caractères littérale 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 numéro de ligne du fichier source, une constante entière 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__ |
développe la date de la traduction, une chaîne de caractères littérale de la forme "mmm jj aaaa". Le nom du mois comme c'est le cas généré 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 chaîne de caractères littérale 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 supplémentaires peuvent être prédéfinis par les implémentations .
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__ |
définie par l'implémentation de valeur, le cas échéant 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) |
définie par l'implémentation de valeur, le cas échéant 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 développe en une constante entière de la yyyymmL forme, si wchar_t utilise Unicode, la date indique la dernière révision 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) |
développe pour 1 si l'encodage de caractères larges du jeu de caractères de base peut ne pas correspondre leur codage étroite Original: expands to 1 if wide character encoding of the basic character set may not equal their narrow encoding The 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) |
développe pour 1 si la mise en œuvre a std::pointer_safety stricte Original: expands to 1 if the implementation has strict std::pointer_safety The 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) |
développe pour 1 si le programme peut avoir plus d'un thread d'exécution Original: expands to 1 if the program can have more than one thread of execution 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 valeurs de ces macros (sauf pour
__FILE__
et __LINE__
) reste constante tout au long de l'unité de traduction. Les tentatives visant à redéfinir ou annuler la définition de ces macros résultat en un comportement indéfini .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 spéciale variable locale prédéfinie nommée __func__ (depuis C++11), défini comme un tableau de caractères statique contenant le nom de la fonction dans la mise en œuvre défini par format. Ce n'est pas une macro du préprocesseur, 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.
[modifier] 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 }
Résultat :
abcd: 12 fff: 2 kkk: 23 34 million