Namespaces
Variants
Views
Actions

va_copy

From cppreference.com
< cpp‎ | utility‎ | variadic
Revision as of 11:46, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <cstdarg>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
void va_copy( va_list dest, va_list src );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

The va_copy macro copies src to dest.

va_end should be called on dest before the function returns or any subsequent re-initialization of dest (via calls to va_start or va_copy).

Contents

Parameters

dest - an instance of the va_list type to initialize
src - the source va_list that will be used to initialize dest

Expanded value

(none)

Example

#include <iostream>
#include <cstdarg>
#include <cmath>
 
double sample_stddev(int count, ...) 
{
    double sum = 0;
    va_list args1;
    va_start(args1, count);
    va_list args2;
    va_copy(args2, args1);
    for (int i = 0; i < count; ++i) {
        double num = va_arg(args1, double);
        sum += num;
    }
    va_end(args1);
    double mean = sum / count;
 
    double sum_sq_diff = 0;
    for (int i = 0; i < count; ++i) {
        double num = va_arg(args2, double);
        sum_sq_diff += (num-mean) * (num-mean);
    }
    va_end(args2);
    return std::sqrt(sum_sq_diff / count);
}
 
int main() 
{
    std::cout << sample_stddev(4, 25.0, 27.3, 26.9, 25.7) << '\n';
}

Output:

0.920258

See also

enables access to variadic function arguments
(function macro) [edit]
accesses the next variadic function argument
(function macro) [edit]
ends traversal of the variadic function arguments
(function macro) [edit]