Espaces de noms
Variantes
Affichages
Actions

Variadic functions

De cppreference.com
< cpp‎ | utility

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.

Sommaire

[modifier] D'utilisation

Pour déclarer une fonction variadique, une ellipse est utilisé comme dernier paramètre, par exemple, int printf(const char *format, ...);. Les paramètres passés à une fonction variadique peut être consulté à l'aide des macros et modèles 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.
Defined in header <cstdarg>
permet d'accéder 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) [edit]
accède à 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) [edit]
(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) [edit]
termine la traversée 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) [edit]
détient les informations nécessaires 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) [edit]

[modifier] Conversions par défaut

Quand une fonction est appelée variadique, après lvalue-à-rvalue, array-à-pointeur et la fonction pointeur-à-conversions, chaque argument qui fait partie de la liste d'argument variable subit des conversions supplémentaires dénommées promotions arguments par défaut " ':
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.
  • std::nullptr_t est converti en void*
    Original:
    std::nullptr_t is converted to void*
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • float arguments sont convertis en double comme dans virgule flottante promotion
    Original:
    float arguments are converted to double as in virgule flottante promotion
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • bool, char, énumérations short, et sans portée sont convertis en int ou plus larges types entiers comme dans promotion entier
    Original:
    bool, char, short, and unscoped enumerations are converted to int or wider integer types as in promotion entier
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Seulement arithmétique, dénombrement, pointeur, le pointeur de membre, et les arguments de type de classe sont autorisés .
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.

[modifier] Alternatives

  • Variadique modèles peuvent également être utilisés pour créer 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 intégrales et en virgule flottante, et sont de type sécurisé. (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 mécanisme commode (mais avec une syntaxe différente) pour accéder 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.

[modifier] 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); 
}

Résultat :

3
a
1.999
42.5