Difference between revisions of "cpp/utility/variadic"
From cppreference.com
(adding structure) |
(added note about initializer lists) |
||
Line 29: | Line 29: | ||
===Alternatives=== | ===Alternatives=== | ||
− | 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. {{mark since c++11}} | + | * 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. {{mark since c++11}} |
+ | * If all variable arguments share a common type, a {{c|std::initializer_list}} provides a convenient mechanism (albeit with a different syntax) for accessing variable arguments. |
Revision as of 08:54, 3 June 2012
Template:cpp/utility/variadic/sidebar
Variadic functions are functions (e.g. std::printf) which take a variable number of arguments.
Usage
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:
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 (class) |
Default conversions
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:
- std::nullptr_t is converted to void*
- float arguments are converted to double as in floating-point promotion
- bool, char, short, and unscoped enumerations are converted to int or wider integer types as in integer promotion
Only arithmetic, enumeration, pointer, pointer to member, and class type arguments are allowed.
Alternatives
- 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. (since C++11)
- 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.