[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/utility/variadic [Back]  [Original]

Variadic functions cppreference.com
cppreference.com
Espaces de noms
Variantes

Variadic functions

De cppreference.com

<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.

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.
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) [edit]
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) [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 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) [edit]
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) [edit]

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.
  • 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, numrations short, et sans porte 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 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.

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

Web Proxy Viewer  |  New URL  |  Original Page