Namespace
Varianti

Variadic functions

Da cppreference.com.
< cpp‎ | utility

 
 
Utilità libreria
Tipo di supporto (basic types, RTTI, type traits)
Gestione della memoria dinamica
La gestione degli errori
Programma di utilità
Funzioni variadic
Data e ora
Funzione oggetti
initializer_list(C++11)
bitset
hash(C++11)
Gli operatori relazionali
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Coppie e tuple
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
Swap, in avanti e spostare
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
 
Variadic funzioni sono funzioni (std::printf ad esempio) che prendono un numero variabile di argomenti.
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.

Indice

[modifica] Utilizzo

Per dichiarare una funzione variadic, i puntini di sospensione viene utilizzato come ultimo parametro, ad esempio, int printf(const char *format, ...);. I parametri passati ad una funzione variadic è possibile accedere utilizzando le seguenti macro e tipi:
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.
Definizione nell'header <cstdarg>
consente l'accesso agli argomenti della funzione variadic
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.

(funzione macro) [modifica]
accede l'argomento successivo funzione variadic
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.

(funzione macro) [modifica]
(C++11)
makes a copy of the variadic function arguments
(funzione macro) [modifica]
termina attraversamento degli argomenti della funzione variadic
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.

(funzione macro) [modifica]
contiene le informazioni necessarie per va_start, va_arg, va_end e 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) [modifica]

[modifica] Predefinite conversioni

Quando una funzione variadic è chiamato, dopo lvalue a rvalue, array a puntatore, e la funzione a puntatore conversioni, ogni argomento che fa parte della lista di argomenti variabile subisce conversioni aggiuntive conosciute come le promozioni di default argomento ' ':
Original:
When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversioni, 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 viene convertito 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 argomenti vengono convertiti in double come virgola mobile promozione
    Original:
    float arguments are converted to double as in virgola mobile promozione
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • bool, char, enumerazioni short, e senza ambito vengono convertiti in int o più ampi tipi interi come in promozione integer
    Original:
    bool, char, short, and unscoped enumerations are converted to int or wider integer types as in promozione integer
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Solo l'aritmetica, l'enumerazione, puntatore, puntatore a membro, e argomenti di tipo di classe sono ammessi.
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.

[modifica] Alternative

  • Modelli variadic può anche essere utilizzato per creare funzioni che accettano numero variabile di argomenti. Essi sono spesso la scelta migliore, perché non imporre restrizioni sui tipi di argomenti, non effettuare promozioni integrali e in virgola mobile, e sono di tipo sicuro. (dal 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. (dal C++11)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Se tutti gli argomenti variabili condividono un tipo comune, un std::initializer_list offre un metodo comodo (anche se con una sintassi diversa) per l'accesso ai argomenti variabili.
    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.

[modifica] Esempio

#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); 
}

Output:

3
a
1.999
42.5