Variadic functions
Aus 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. |
Variadic sind Funktionen (zB std::printf), die eine variable Anzahl von Argumenten zu nehmen .
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.
Inhaltsverzeichnis |
[Bearbeiten] Usage
Um eine variadische Funktion deklarieren, wird eine Ellipse als letzten Parameter, zB verwendet int printf(const char *format, ...);. Übergebenen Parameter einer variadische Funktion kann über die folgenden Makros und Typen werden:
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.
definiert in Header
<cstdarg> | |
ermöglicht den Zugriff auf variadische Funktionsargumente 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. (funktionieren Makro) | |
greift auf die nächsten variadische Funktionsargument 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. (funktionieren Makro) | |
(C++11) |
makes a copy of the variadic function arguments (funktionieren Makro) |
endet Durchlaufen der variadische Funktionsargumente 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. (funktionieren Makro) | |
hält die Informationen va_start, va_arg, va_end und va_copy benötigt 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. (Klasse) |
[Bearbeiten] Standard Konvertierungen
Wenn ein variadische Funktion aufgerufen wird, nachdem lvalue-to-rvalue, array-to-Zeiger und Funktion-to-Zeiger Konvertierungen, jedes Argument, das ein Teil der variablen Liste der Argumente zur erfährt zusätzliche Konvertierungen als Standard-Argument Promotionen bekannt ' ':
Original:
When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointer Konvertierungen, 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 wird void* umgewandeltOriginal: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 Argumente werden double wie in Floating-Point-Förderung umgewandeltOriginal:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - bool, char, short und ohne Bereichseinschränkung Aufzählungen sind int oder breiter Integer-Typen wie in integer Förderung umgewandeltOriginal:bool, char, short, and unscoped enumerations are converted to int or wider integer types as in integer FörderungThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Nur Arithmetik, Aufzählung, Zeiger, Zeiger auf ein Element, und die Klasse Typargumente sind erlaubt .
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.
[Bearbeiten] Alternativen
- Variadic Vorlagen können auch verwendet werden, um Funktionen, die variablen Anzahl von Argumenten nehmen zu schaffen. Sie sind oft die bessere Wahl, weil sie nicht durchsetzen, Einschränkungen für die Typen der Argumente, nicht durchführen Integral-und Floating-Point-Aktionen und sind typsicher. (seit 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. (seit C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Wenn alle variable Argumente einen gemeinsamen Typ Aktie bietet ein std::initializer_list einen bequemen Mechanismus (wenn auch mit einer anderen Syntax) für den Zugriff auf variable Argumente .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.
[Bearbeiten] Beispiel
#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