diff --git a/docs/code-quality/c26826.md b/docs/code-quality/c26826.md new file mode 100644 index 00000000000..02d077007ef --- /dev/null +++ b/docs/code-quality/c26826.md @@ -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`