General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
Allows a function to accept any number of extra arguments.
A function is a variadic if the last parameter of itsparameter list is an ellipsis (...).
The comma preceding the ellipsis can be omitted. | (deprecated in C++26) |
// the function declared as followsint printx(constchar* fmt, ...);int printx(constchar* fmt...);// same as above, but deprecated since C++26 // may be called with one or more arguments:printx("hello world");printx("a=%d b=%d", a, b); int printy(...,constchar* fmt);// error: ... can only be the last parameterint printz(...);// valid, but the arguments cannot be accessed portably
This is different from a functionparameter pack expansion, which is indicated by an ellipsis that is a part of a parameter declarator, rather than an ellipsis being a parameter alone. Both parameter pack expansion and the “variadic” ellipsis may appear in the declaration of a function template, as in the case ofstd::is_function. | (since C++11) |
Contents |
When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointerconversions, each argument that is a part of the variable argument list undergoes additional conversions known asdefault argument promotions:
| (since C++11) |
Non-POD class types(until C++11)Scoped enumerations and class types with an eligible non-trivial copy constructor, an eligible non-trivial move constructor, or a non-trivial destructor(since C++11) are conditionally-supported in potentially-evaluated calls with implementation-defined semantics (these types are always supported inunevaluated calls).
Because variadic parameters have the lowest rank for the purpose ofoverload resolution, they are commonly used as the catch-all fallbacks inSFINAE.
Within the body of a function that uses variadic arguments, the values of these arguments may be accessed using the<cstdarg>
library facilities:
Defined in header <cstdarg> | |
enables access to variadic function arguments (function macro)[edit] | |
accesses the next variadic function argument (function macro)[edit] | |
(C++11) | makes a copy of the variadic function arguments (function macro)[edit] |
ends traversal of the variadic function arguments (function macro)[edit] | |
holds the information needed byva_start,va_arg,va_end, andva_copy (typedef)[edit] |
The behavior of theva_start macro is undefined if the last parameter before the ellipsis has reference type, or has type that is notcompatible with the type that results from default argument promotions.
If the apack expansion or an entity resulting from alambda capture is used as the last parameter inva_start, the program is ill-formed, no diagnostic required. | (since C++11) |
| (since C++11) |
In the C programming language until C23, at least one named parameter must appear before the ellipsis parameter, soR printz(...); is not valid until C23. In C++, this form is allowed even though the arguments passed to such function are not accessible, and is commonly used as the fallback overload inSFINAE, exploiting the lowest priority of the ellipsis conversion inoverload resolution.
This syntax for variadic arguments was introduced in 1983 C++ without the comma before the ellipsis. When C89 adopted function prototypes from C++, it replaced the syntax with one requiring the comma. For compatibility, C++98 accepts both C++-stylef(int n...) and C-stylef(int n, ...). The original C++-style grammar is deprecated since C++26.
The comma can be used in abbreviated function templates to make the ellipsis signify a variadic function instead of a variadic template: void f1(auto...); // same as template<class... Ts> void f3(Ts...) | (since C++20) |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 506 | C++98 | passing non-POD class arguments to an ellipsis resulted in undefined behavior | passing such arguments is conditionally-supported with implementation-defined semantics |
CWG 634 | C++98 | conditionally-supported class types made some SFINAE idioms not work | always supported if unevaluated |
CWG 2247 | C++11 | no restriction on passing parameter pack or lambda capture to va_start | made ill-formed, no diagnostic required |
CWG 2347 | C++11 | it was unclear whether scoped enumerations passed to an ellipsis are subject to default argument promotions | passing scoped enumerations is conditionally-supported with implementation-defined semantics |
C documentation forVariadic arguments | |
C documentation forImplicit conversions |