Skip to content

Add description for new warning C26826 #3474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/code-quality/c26826.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: C26826
description: "Reference for Microsoft C++ Code Analysis warning C26826 in Visual Studio."
ms.date: 10/25/2021
f1_keywords: ["C26826"]
helpviewer_keywords: ["C26826"]
---
# C26826

> Don't use C-style variable arguments (f.55).

For more information, see [F.55: Don't use `va_arg` arguments](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#F-varargs) 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

```cpp
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` arguments
- `std::initializer_list`