Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/variadic/va start"

From cppreference.com
< cpp‎ | utility‎ | variadic
m (fmt)
(apply 18.10[support.runtime]/3 instead of C rules)
Line 12: Line 12:
 
{{tt|va_start}} should be invoked with an instance to a valid {{lc|va_list}} object {{tt|ap}} before any calls to {{lc|va_arg}}.
 
{{tt|va_start}} should be invoked with an instance to a valid {{lc|va_list}} object {{tt|ap}} before any calls to {{lc|va_arg}}.
  
If {{tt|parm_n}} is declared with reference type, with {{tt|register}} storage class specifier, or with a type not compatible with the type that results from default argument promotions, the behavior is undefined.
+
If {{tt|parm_n}} is declared with reference type or with 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===
Line 22: Line 22:
 
===Expanded value===
 
===Expanded value===
 
(none)
 
(none)
 +
 +
===Notes===
 +
{{tt|va_start}} is required to support {{tt|parm_n}} with overloaded {{tt|operator&}}.
  
 
===Example===
 
===Example===

Revision as of 07:51, 17 August 2015

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <cstdarg>
void va_start( 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 parm_n is declared with reference type or with a type not compatible with the type that results from default argument promotions, the behavior is undefined.

Contents

Parameters

ap - an instance of the va_list type
parm_n - the named parameter preceding the first variable parameter

Expanded value

(none)

Notes

va_start is required to support parm_n with overloaded operator&.

Example

#include <iostream>
#include <cstdarg>
 
int add_nums(int count, ...) 
{
    int result = 0;
    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

See also

accesses the next variadic function argument
(function macro) [edit]
ends traversal of the variadic function arguments
(function macro) [edit]
C documentation for va_start