Namespaces
Variants
Views
Actions

Variadic arguments

From cppreference.com
< cpp‎ | language
Revision as of 04:05, 17 March 2021 by DR-ify (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
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
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Allows a function to accept any number of arguments.

Indicated by a trailing ... (other than one introducing a pack expansion)(since C++11) following the parameter-list of a function declaration.

When the parameter-list is not empty, an optional comma may precede a ... signifying a variadic function. This provides compatibility with C (which added a requirement for a comma when it adopted function prototypes from C++).

// the function declared as follows
int printx(const char* fmt...);
// may be called with one or more arguments:
printx("hello world");
printx("a=%d b=%d", a, b);
 
int printx(const char* fmt, ...); // same as above (extraneous comma is allowed 
                                  // for C compatibility)
int printy(..., const char* fmt); // error: ... cannot appear as a parameter
int printz(...); // valid, but the arguments cannot be accessed portably

Note: this is different from a function parameter pack expansion, which is indicated by an ellipsis that is a part of a parameter declarator, rather than an ellipsis that appears after all parameter declarations. Both parameter pack expansion and the "variadic" ellipsis may appear in the declaration of a function template, as in the case of std::is_function.

(since C++11)

Contents

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:

(since C++11)

Only arithmetic, enumeration, pointer, pointer to member, and class type arguments (after conversion) are allowed. However, non-POD class types(until C++11)classes types with an eligible non-trivial copy constructor, an eligible non-trivial move constructor, or a non-trivial destructor, together with scoped enumerations(since C++11), are conditionally-supported in potentially-evaluated calls with implementation-defined semantics (these types are always supported in unevaluated calls).

Because variadic parameters have the lowest rank for the purpose of overload resolution, they are commonly used as the catch-all fallbacks in SFINAE.

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 by va_start, va_arg, va_end, and va_copy
(typedef) [edit]

The behavior of the va_start macro is undefined if the last parameter before the ellipsis has reference type, or has type that is not compatible with the type that results from default argument promotions.

If the a pack expansion or an entity resulting from a lambda capture is used as the last parameter in va_start, the program is ill-formed, no diagnostic required.

(since C++11)

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.
  • 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. In this case however the arguments cannot be modified since std::initializer_list can only provide a const pointer to its elements.
(since C++11)

Notes

In the C programming language, at least one named parameter must appear before the ellipsis parameter, so printz(...); is not valid. 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 in SFINAE, exploiting the lowest priority of the ellipsis conversion in overload 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++-style f(int n...) and C-style f(int n, ...)

Defect reports

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 a non-POD class argument to an ellipsis resulted in UB made conditionally-supported
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 unclear how scope enumerations are passed to an ellipsis made conditionally-supported

See also

C documentation for Variadic arguments