CKTIS / C++
C-style variadic function: the ellipsis parameter
A trailing ... in a parameter list accepts additional arguments without declared parameter types. The function reads them through <cstdarg>, relying on an agreed count and types; mistakes in that contract can cause undefined behavior.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
return_type function_name(fixed_parameters, ...);The C-style ellipsis parameter is the trailing ... in a declaration such as int sum_ints(int count, ...);. The function accepts the required argument count followed by zero or more additional arguments. Ordinary parameters retain type checking, but the ellipsis specifies neither the types nor the number of extra arguments. It is not a template parameter pack such as Args....
The <cstdarg> header provides std::va_list and macros for sequential access. In the example, va_start(args, count) starts traversal, va_arg(args, int) retrieves the next argument as int, and va_end(args) finishes using the list. count is a contract with the caller, not a count automatically checked by the language. Reading beyond the supplied arguments or using an incompatible type, outside standard-defined exceptions, causes undefined behavior.
Arguments passed through ... undergo default argument promotions: float becomes double, while bool, char, and short undergo integral promotions to int or, where the rules require it, unsigned int. Retrieve the promoted type, for example double, not float. The example uses only int arguments, whose sum fits in int. It uses the two-argument va_start form appropriate for C++23. The supplied sources are a rolling standard draft, not a frozen C++23 text.
Examples
#include <cstdarg>
#include <iostream>
int sum_ints(int count, ...) {
std::va_list args;
va_start(args, count);
int total = 0;
for (int i = 0; i < count; ++i) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
int main() {
std::cout << sum_ints(3, 10, 20, 30) << '\n';
std::cout << sum_ints(0) << '\n';
}
Result
60 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.