小编Ope*_*tor的帖子

How can I use va_arg in a loop without knowing how many optional arguments there are in C++?

I want to write a function which takes at least two integer and returns the sum of all integer passed to the function:

int sumOfAtLeastTwoIntegers(int a, int b, ...){
   
    int sum = a+b;
    va_list ptr;
    va_start(ptr,b);
    for(){
        sum += va_arg(ptr, int)
    }

    va_end(ptr);
    return sum;
}
Run Code Online (Sandbox Code Playgroud)

I want to know how the expression in the for loop has to look like such that the loop continues until all optional arguments were added to the sum. How would I achieve this …

c++ templates variadic-functions variadic-templates

1
推荐指数
2
解决办法
116
查看次数