|
| 1 | +--- |
| 2 | +description: "Learn more about: /Zc:static_assert (Strict static_assert handling)" |
| 3 | +title: "/Zc:static_assert (Strict static_assert handling)" |
| 4 | +ms.date: 12/15/2021 |
| 5 | +f1_keywords: ["/Zc:static_assert"] |
| 6 | +helpviewer_keywords: ["/Zc:static_assert compiler option (C++)"] |
| 7 | +--- |
| 8 | +# `/Zc:static_assert` (Strict static_assert handling) |
| 9 | + |
| 10 | +The **`/Zc:static_assert`** compiler option tells the compiler to evaluate `static_assert` calls with non-dependent test expressions when class or function templates are parsed. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +> **`/Zc:static_assert`**\ |
| 15 | +> **`/Zc:static_assert-`** |
| 16 | +
|
| 17 | +## Remarks |
| 18 | + |
| 19 | +The **`/Zc:static_assert`** compiler option tells the compiler to evaluate a `static_assert` in the body of a function template or in the body of a class template member function when first parsed, if the test expression isn't dependent. If the non-dependent test expression isn't `false`, the compiler emits an error immediately. When the test expression is dependent, the `static_assert` isn't evaluated until the template is instantiated. |
| 20 | + |
| 21 | +The **`/Zc:static_assert`** option is available starting in Visual Studio 2022 version 17.1. In earlier versions of Visual Studio, or if **`/Zc:static_assert-`** is specified, Visual Studio doesn't do dependent analysis if the `static_assert` is within the body of a function template or within the body of a member function of a class template. Instead, it only evaluates the `static_assert` when a template is instantiated. |
| 22 | + |
| 23 | +The **`/permissive-`** option enables **`/Zc:static_assert`**, so it's on by default in projects that use **`/std:c++20`** or **`/std:c++latest`**. The **`/Zc:static_assert-`** option must come after a **`/std:c++20`**, **`/std:c++latest`**, or **`/permissive-`** option on the command line. |
| 24 | + |
| 25 | +If the compiler is in the default C++14 mode and **`/permissive-`** or **`/Zc:static_assert`** is specified, it uses **`/Zc:static_assert`** behavior. However, if it evaluates a `static_assert` in a template body, it also reports off-by-default warning C5254, "language feature 'terse static assert' requires compiler flag '**`/std:c++17`**'", since this behavior isn't required until C++17. |
| 26 | + |
| 27 | +### To set this compiler option in Visual Studio |
| 28 | + |
| 29 | +1. Open the project's **Property Pages** dialog box. For details, see [Set C++ compiler and build properties in Visual Studio](../working-with-project-properties.md). |
| 30 | + |
| 31 | +1. Select the **Configuration Properties** > **C/C++** > **Command Line** property page. |
| 32 | + |
| 33 | +1. Add **`/Zc:static_assert`** or **`/Zc:static_assert-`** to the **Additional options:** pane. |
| 34 | + |
| 35 | +## See also |
| 36 | + |
| 37 | +[`/Zc` (Conformance)](zc-conformance.md) |
0 commit comments