Espacios de nombres
Variantes
Acciones

Variadic functions

De cppreference.com
< cpp‎ | utility

 
 
Utilidades de la biblioteca
Escriba apoyo (basic types, RTTI, type traits)
Gestión de memoria dinámica
Gestión de errores
Programa de servicios públicos
Funciones variadic
Fecha y hora
Objetos Function
initializer_list(C++11)
bitset
hash(C++11)
Operadores relacionales
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>=
Pares y tuplas
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)
Cambiar, hacia adelante y mover
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 funciones son funciones (std::printf, por ejemplo) que tienen un número variable de argumentos .
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.

Contenido

[editar] Usage

Para declarar una función variadic, los puntos suspensivos se usa como el último parámetro, por ejemplo, int printf(const char *format, ...);. Los parámetros pasados ​​a una función variadic se puede acceder usando las macros y los modelos siguientes:
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>
permite el acceso a los argumentos de la función 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.

(función macro) [edit]
accede a la función siguiente argumento 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.

(función macro) [edit]
(C++11)
makes a copy of the variadic function arguments
(función macro) [edit]
termina recorrido de los argumentos de la función 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.

(función macro) [edit]
contiene la información necesaria para va_start, va_arg va_end y 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.

(clase) [edit]

[editar] Conversiones predeterminadas

Cuando una función variadic se llama, después de valor-i-a-valor p, matriz a puntero, y conversiones función a puntero, cada argumento de que es una parte de la lista de argumentos variable se somete a conversiones adicionales llamados promociones predeterminados argumento ' ':
Original:
When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversiones, 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.
Sólo aritmética, enumeración, puntero, puntero a miembro, y los argumentos de la clase de tipo se admiten .
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.

[editar] Alternativas

  • Variadic plantillas también se puede utilizar para crear funciones que toman número variable de argumentos. Ellos son a menudo la mejor opción, ya que no imponen restricciones a los tipos de los argumentos, no realice promociones integral y de punto flotante, y son de tipo seguro. (desde 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. (desde 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 todos los argumentos variables comparten un tipo común, un std::initializer_list proporciona un mecanismo conveniente (aunque con una sintaxis diferente) para acceder a argumentos 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.

[editar] Ejemplo

#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