Skip to content

Commit 883bdad

Browse files
author
Colin Robertson
authored
Merge pull request #3474 from dmitrykobets-msft/dmitrykobets-c26826
Add description for new warning C26826
2 parents 83fd06f + 12354c0 commit 883bdad

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

docs/code-quality/c26826.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#F-varargs) in the C++ Core Guidelines.
13+
14+
## Remarks
15+
16+
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.
17+
18+
Warning C26826 is available starting in Visual Studio 2022 version 17.1.
19+
20+
## Example
21+
22+
```cpp
23+
int sum(int n, ...) {
24+
va_list l; // C26826 Don't use C-style variable arguments
25+
va_start(l, n); // C26826 Don't use C-style variable arguments
26+
27+
int s = 0;
28+
for (int i = 0; i < n; ++i) {
29+
// BAD, assumes the variable arguments will be passed as ints
30+
s += va_arg(l, int); // C26826 Don't use C-style variable arguments
31+
}
32+
33+
va_end(l); // C26826 Don't use C-style variable arguments
34+
return s;
35+
}
36+
37+
int main() {
38+
sum(2, 1, 2, 3); // ok
39+
sum(2, 1.5, 3.14159, 2.71828); // BAD, undefined
40+
}
41+
```
42+
43+
Alternatives to C-style variable arguments include:
44+
- function overloading
45+
- variadic templates
46+
- `std::variant` arguments
47+
- `std::initializer_list`

0 commit comments

Comments
 (0)