Difference between revisions of "cpp/utility/variadic/va start"
From cppreference.com
(return -> expanded) |
(Added LWG issue #2099 DR.) |
||
(20 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ctitle | + | {{ctitle|va_start}} |
− | {{cpp/utility/variadic/ | + | {{cpp/utility/variadic/navbar}} |
− | {{ddcl | void va_start(va_list ap, parm_n);}} | + | {{ddcl|header=cstdarg| |
+ | void va_start( std::va_list ap, parm_n ); | ||
+ | }} | ||
− | The {{tt|va_start}} macro enables access to the variable arguments following the named argument {{ | + | The {{tt|va_start}} macro enables access to the variable arguments following the named argument {{c|parm_n}}. |
− | {{tt|va_start}} should be invoked with an instance to a valid {{ | + | {{tt|va_start}} should be invoked with an instance to a valid {{lc|va_list}} object {{c|ap}} before any calls to {{lc|va_arg}}. |
+ | |||
+ | {{rrev|since=c++11| | ||
+ | If the {{c|parm_n}} is a {{lsd|cpp/language/parameter pack#Pack expansion}} or an entity resulting from a {{lsd|cpp/language/lambda#Lambda capture}}, the program is ill-formed, no diagnostic required. | ||
+ | }} | ||
+ | |||
+ | If {{c|parm_n}} is of reference type, or of a type not compatible with the type that results from [[cpp/language/variadic arguments#Default conversions|default argument promotions]], the behavior is undefined. | ||
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|ap|an object of the {{lc|va_list}} type}} |
− | {{ | + | {{par|parm_n|the named parameter preceding the first variable parameter}} |
− | {{ | + | {{par end}} |
===Expanded value=== | ===Expanded value=== | ||
(none) | (none) | ||
+ | |||
+ | ===Notes=== | ||
+ | {{tt|va_start}} is required to support {{c|parm_n}} with overloaded {{tt|operator&}}. | ||
+ | |||
+ | ===Example=== | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <cstdarg> | ||
+ | #include <iostream> | ||
+ | |||
+ | int add_nums(int count...) | ||
+ | { | ||
+ | int result = 0; | ||
+ | std::va_list args; | ||
+ | va_start(args, count); | ||
+ | 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=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=cwg|dr=273|std=C++98|before=it was unclear whether {{tt|va_start}} is required to<br>support {{c|parm_n}}s with overloaded {{tt|operator&}}|after=required}} | ||
+ | {{dr list item|wg=lwg|dr=2099|std=C++98|before=the behavior was undefined if {{c|parm_n}} is<br>declared with a function, array, or reference type|after=the behavior is undefined if<br>{{c|parm_n}} is of reference type}} | ||
+ | {{dr list end}} | ||
===See also=== | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc inc|cpp/utility/variadic/dsc va_arg}} | ||
+ | {{dsc inc|cpp/utility/variadic/dsc va_end}} | ||
+ | {{dsc see c|c/variadic/va_start}} | ||
+ | {{dsc end}} | ||
− | {{ | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
− | + | ||
− | + | ||
− | + |
Latest revision as of 22:10, 25 February 2024
Defined in header <cstdarg>
|
||
void va_start( std::va_list ap, parm_n ); |
||
The va_start
macro enables access to the variable arguments following the named argument parm_n.
va_start
should be invoked with an instance to a valid va_list object ap before any calls to va_arg.
If the parm_n is a pack expansion or an entity resulting from a lambda capture, the program is ill-formed, no diagnostic required. |
(since C++11) |
If parm_n is of reference type, or of a type not compatible with the type that results from default argument promotions, the behavior is undefined.
Contents |
[edit] Parameters
ap | - | an object of the va_list type |
parm_n | - | the named parameter preceding the first variable parameter |
[edit] Expanded value
(none)
[edit] Notes
va_start
is required to support parm_n with overloaded operator&
.
[edit] Example
Run this code
Output:
150
[edit] 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 is declared with a function, array, or reference type |
the behavior is undefined if parm_n is of reference type |
[edit] See also
accesses the next variadic function argument (function macro) | |
ends traversal of the variadic function arguments (function macro) | |
C documentation for va_start
|