Skip to content

Commit 6ba6d45

Browse files
Add description for new warning C26826
1 parent 82dd8d2 commit 6ba6d45

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/code-quality/c26826.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: C26826
3+
description: "Reference for Microsoft C++ Code Analysis warning C26826 in Visual Studio."
4+
ms.date: 10/25/2021
5+
f1_keywords: ["C26826"]
6+
helpviewer_keywords: ["C26826"]
7+
---
8+
# C26826
9+
10+
> Don't use C-style variable arguments (f.55).
11+
12+
For more information, see [F.55: Don't use `va_arg` arguments](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#f55-dont-use-va_arg-arguments) in the C++ Core Guidelines.
13+
14+
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 / read with the correct types.
15+
16+
## Example
17+
18+
```cpp
19+
int sum(int n, ...) {
20+
va_list l; // C26826 Don't use C-style variable arguments
21+
va_start(l, n); // C26826 Don't use C-style variable arguments
22+
23+
int s = 0;
24+
for (int i = 0; i < n; ++i) {
25+
// BAD, assumes the variable arguments will be passed as ints
26+
s += va_arg(l, int); // C26826 Don't use C-style variable arguments
27+
}
28+
29+
va_end(l); // C26826 Don't use C-style variable arguments
30+
return s;
31+
}
32+
33+
int main() {
34+
sum(2, 1, 2, 3); // ok
35+
sum(2, 1.5, 3.14159, 2.71828); // BAD, undefined
36+
}
37+
```
38+
39+
Alternatives to C-style variable arguments include:
40+
- function overloading
41+
- variadic templates
42+
- `std::variant` arguments
43+
- `std::initializer_list`
44+

0 commit comments

Comments
 (0)