va_start
| Defined in header <cstdarg>
|
||
void va_start( std::va_list ap, parmN );
|
(until C++26) | |
void va_start( std::va_list ap, ... );
|
(since C++26) | |
The va_start macro enables access to the variable argumentsfollowing the named argument parmN(until C++26).
va_start shall be invoked with an instance to a valid std::va_list object ap before any calls to va_arg.
If |
(until C++26) | ||
|
If more than one argument is present for |
(since C++26) |
Parameters
| ap | - | an object of the va_list type |
| parm_n | - | the named parameter preceding the first variable parameter |
Expanded value
(none)
Notes
|
|
(until C++26) |
Example
#include <cstdarg>
#include <iostream>
int add_nums(int count...)
{
int result = 0;
std::va_list args;
va_start(args, count); // count can be omitted since C++26
for (int i = 0; i < count; ++i)
result += va_arg(args, int);
va_end(args);
return result;
}
int main()
{
std::cout << add_nums(4, 25, 25, 50, 50) << '\n';
}
Output:
150
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 273 | C++98 | it was unclear whether va_start is required tosupport parm_ns with overloaded operator&
|
required |
| LWG 2099 | C++98 | the behavior was undefined if parm_n isdeclared with a function, array, or reference type |
the behavior is undefined ifparm_n is of reference type
|
See also
| accesses the next variadic function argument (function macro) | |
| ends traversal of the variadic function arguments (function macro) | |
C documentation for va_start
| |