Variadic functions
De cppreference.com
[] |
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/>
Fonctions variadic sont des fonctions (std::printf par exemple) qui prennent un nombre variable d'arguments .
Original:
Variadic functions are functions (e.g. std::printf) which take a variable number of arguments.
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.
D'utilisation
Pour dclarer une fonction variadique, une ellipse est utilis comme dernier paramtre, par exemple,
int printf(const char *format, ...);. Les paramtres passs une fonction variadique peut tre consult l'aide des macros et modles suivants:Original:
To declare a variadic function, an ellipsis is used as the last parameter, e.g.
int printf(const char *format, ...);. Parameters passed to a variadic function can be accessed using the following macros and types: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.
Defined in header
<cstdarg> | |
permet d'accder aux arguments de la fonction variadique Original: enables access to variadic function arguments The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction macro) | |
accde l'argument de la fonction suivante variadique Original: accesses the next variadic function argument The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction macro) | |
(C++11) |
fait une copie des arguments de la fonction variadique Original: makes a copy of the variadic function arguments The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction macro) |
termine la traverse des arguments de la fonction variadique Original: ends traversal of the variadic function arguments The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction macro) | |
dtient les informations ncessaires par va_start, va_arg, va_end, et va_copy Original: holds the information needed by va_start, va_arg, va_end, and va_copy The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (classe) | |
Conversions par dfaut
Quand une fonction est appele variadique, aprs lvalue--rvalue, array--pointeur et la fonction pointeur--conversions, chaque argument qui fait partie de la liste d'argument variable subit des conversions supplmentaires dnommes promotions arguments par dfaut " ':
Original:
When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversions, each argument that is a part of the variable argument list undergoes additional conversions known as default argument promotions:
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.
- std::nullptr_t est converti en
void*Original:std::nullptr_t is converted tovoid*The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. floatarguments sont convertis endoublecomme dans virgule flottante promotionOriginal:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.bool,char, numrationsshort, et sans porte sont convertis enintou plus larges types entiers comme dans promotion entierOriginal:bool,char,short, and unscoped enumerations are converted tointor wider integer types as in promotion entierThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Seulement arithmtique, dnombrement, pointeur, le pointeur de membre, et les arguments de type de classe sont autoriss .
Original:
Only arithmetic, enumeration, pointer, pointer to member, and class type arguments are allowed.
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.
Alternatives
- Variadique modles peuvent galement tre utiliss pour crer des fonctions qui prennent un nombre variable d'arguments. Ils sont souvent le meilleur choix car ils n'imposent pas de restrictions sur les types des arguments, ne pas effectuer promotions intgrales et en virgule flottante, et sont de type scuris. (depuis C++11)Original:Variadic templates can also be used to create functions that take variable number of arguments. They are often the better choice because they do not impose restrictions on the types of the arguments, do not perform integral and floating-point promotions, and are type safe. (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. - Si tous les arguments variables partagent un type commun, un std::initializer_list fournit un mcanisme commode (mais avec une syntaxe diffrente) pour accder aux arguments variables .Original:If all variable arguments share a common type, a std::initializer_list provides a convenient mechanism (albeit with a different syntax) for accessing variable arguments.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Exemple
#include <iostream>
#include <cstdarg>
void simple_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
while (*fmt != '\0') {
if (*fmt == 'd') {
int i = va_arg(args, int);
std::cout << i << '\n';
} else if (*fmt == 'c') {
// note automatic conversion to integral type
int c = va_arg(args, int);
std::cout << static_cast<char>(c) << '\n';
} else if (*fmt == 'f') {
double d = va_arg(args, double);
std::cout << d << '\n';
}
++fmt;
}
va_end(args);
}
int main()
{
simple_printf("dcff", 3, 'a', 1.999, 42.5);
}
Rsultat :
3
a
1.999
42.5