Difference between revisions of "cpp/utility/variadic"
From cppreference.com
(moved conversions and alternatives to the language page) |
Andreas Krug (Talk | contribs) m (fmt) |
||
(16 intermediate revisions by 11 users not shown) | |||
Line 4: | Line 4: | ||
Variadic functions are functions (e.g. {{lc|std::printf}}) which take a [[cpp/language/variadic_arguments|variable number of arguments]]. | Variadic functions are functions (e.g. {{lc|std::printf}}) which take a [[cpp/language/variadic_arguments|variable number of arguments]]. | ||
− | To declare a variadic function, an ellipsis | + | To declare a variadic function, an ellipsis appears after the list of parameters, e.g. {{c|int printf(const char* format...);}}, which may be preceded by an optional comma. See [[cpp/language/variadic_arguments|Variadic arguments]] for additional detail on the syntax, automatic argument conversions and the alternatives. |
To access the variadic arguments from the function body, the following library facilities are provided: | To access the variadic arguments from the function body, the following library facilities are provided: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc header | cstdarg}} | + | {{dsc header|cstdarg}} |
− | {{dsc inc | cpp/utility/variadic/dsc va_start}} | + | {{dsc inc|cpp/utility/variadic/dsc va_start}} |
− | {{dsc inc | cpp/utility/variadic/dsc va_arg}} | + | {{dsc inc|cpp/utility/variadic/dsc va_arg}} |
− | {{dsc inc | cpp/utility/variadic/dsc va_copy}} | + | {{dsc inc|cpp/utility/variadic/dsc va_copy}} |
− | {{dsc inc | cpp/utility/variadic/dsc va_end}} | + | {{dsc inc|cpp/utility/variadic/dsc va_end}} |
− | {{dsc inc | cpp/utility/variadic/dsc va_list}} | + | {{dsc inc|cpp/utility/variadic/dsc va_list}} |
{{dsc end}} | {{dsc end}} | ||
===Example=== | ===Example=== | ||
− | |||
{{example | {{example | ||
− | + | |code= | |
− | + | ||
− | + | ||
#include <cstdarg> | #include <cstdarg> | ||
+ | #include <iostream> | ||
− | void simple_printf(const char* fmt...) | + | void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid |
{ | { | ||
va_list args; | va_list args; | ||
va_start(args, fmt); | va_start(args, fmt); | ||
− | while (*fmt != '\0') { | + | while (*fmt != '\0') |
− | if (*fmt == 'd') { | + | { |
+ | if (*fmt == 'd') | ||
+ | { | ||
int i = va_arg(args, int); | int i = va_arg(args, int); | ||
std::cout << i << '\n'; | std::cout << i << '\n'; | ||
− | } else if (*fmt == 'c') { | + | } |
+ | else if (*fmt == 'c') | ||
+ | { | ||
// note automatic conversion to integral type | // note automatic conversion to integral type | ||
int c = va_arg(args, int); | int c = va_arg(args, int); | ||
std::cout << static_cast<char>(c) << '\n'; | std::cout << static_cast<char>(c) << '\n'; | ||
− | } else if (*fmt == 'f') { | + | } |
+ | else if (*fmt == 'f') | ||
+ | { | ||
double d = va_arg(args, double); | double d = va_arg(args, double); | ||
std::cout << d << '\n'; | std::cout << d << '\n'; | ||
Line 52: | Line 56: | ||
simple_printf("dcff", 3, 'a', 1.999, 42.5); | simple_printf("dcff", 3, 'a', 1.999, 42.5); | ||
} | } | ||
− | + | |output= | |
3 | 3 | ||
a | a | ||
Line 59: | Line 63: | ||
}} | }} | ||
− | + | ===See also=== | |
− | + | {{dsc begin}} | |
− | + | {{dsc see c|c/variadic|Variadic functions|nomono=true}} | |
− | + | {{dsc end}} | |
− | + | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + |
Latest revision as of 10:50, 3 January 2024
Variadic functions are functions (e.g. std::printf) which take a variable number of arguments.
To declare a variadic function, an ellipsis appears after the list of parameters, e.g. int printf(const char* format...);, which may be preceded by an optional comma. See Variadic arguments for additional detail on the syntax, automatic argument conversions and the alternatives.
To access the variadic arguments from the function body, the following library facilities are provided:
Defined in header
<cstdarg> | |
enables access to variadic function arguments (function macro) | |
accesses the next variadic function argument (function macro) | |
(C++11) |
makes a copy of the variadic function arguments (function macro) |
ends traversal of the variadic function arguments (function macro) | |
holds the information needed by va_start, va_arg, va_end, and va_copy (typedef) |
[edit] Example
Run this code
#include <cstdarg> #include <iostream> void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid { 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
[edit] See also
C documentation for Variadic functions
|