Warning C26826
Don't use C-style variable arguments (f.55).
For more information, see F.55: Don't use va_arg
arguments in the C++ Core Guidelines.
Remarks
This check warns on all usages of va_list
, va_start
, va_arg
, and va_end
, discouraging the use of C-style variable arguments. C-style variable arguments are unsafe because they require the programmer to assume that the arguments are all passed and read with the correct types.
Warning C26826 is available starting in Visual Studio 2022 version 17.1.
Example
int sum(int n, ...) {
va_list l; // C26826 Don't use C-style variable arguments
va_start(l, n); // C26826 Don't use C-style variable arguments
int s = 0;
for (int i = 0; i < n; ++i) {
// BAD, assumes the variable arguments will be passed as ints
s += va_arg(l, int); // C26826 Don't use C-style variable arguments
}
va_end(l); // C26826 Don't use C-style variable arguments
return s;
}
int main() {
sum(2, 1, 2, 3); // ok
sum(2, 1.5, 3.14159, 2.71828); // BAD, undefined
}
Alternatives to C-style variable arguments include:
- function overloading
- variadic templates
std::variant
argumentsstd::initializer_list