You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/c-language/generic_selection.md
+5-7Lines changed: 5 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,15 @@
1
1
---
2
2
title: "Generic selection (C11)"
3
3
description: "Describes the C11 _Generic keyword used in the Microsoft Visual C compiler"
4
-
ms.date: "12/8/2020"
4
+
ms.date: "12/9/2020"
5
5
helpviewer_keywords: ["_Generic keyword [C]"]
6
6
---
7
7
8
8
# Generic selection (C11)
9
9
10
-
Use the **`_Generic`** keyword to write code that makes a compile-time decision based on the type of the argument.
10
+
Use the **`_Generic`** keyword to write code that selects an expression at compiletime based on the type of the argument. It's similar to overloading in C++ where the type of the argument selects which function to call, except that the type of the argument selects which expression to evaluate.
11
11
12
-
**`_Generic`** selects an expression based on a type at compile time. It's similar to overloading in C++ where the type of the argument selects which function to call, except that the type of the argument selects which expression to evaluate.
13
-
14
-
For example, the expression `_Generic(42, int: "integer", char: "character", default: "unknown");` evaluates the type of `42` and looks for the matching type, `int` in the list. It will find it and return `"integer"`.
12
+
For example, the expression `_Generic(42, int: "integer", char: "character", default: "unknown");` evaluates the type of `42` and looks for the matching type, `int`, in the list. It finds it and returns `"integer"`.
15
13
16
14
## Syntax
17
15
@@ -26,9 +24,9 @@ For example, the expression `_Generic(42, int: "integer", char: "character", def
The first *`assignment-expression`* is called the controlling expression. The controlling expression is evaluated at compile time and matched against the *`assoc-list`* to find which expression to evaluate and return.
27
+
The first *`assignment-expression`* is called the controlling expression. The type of the controlling expression is determined at compile time and matched against the *`assoc-list`* to find which expression to evaluate and return. The controlling expression is not evaluated. For example, `_Generic(intFunc(), int: "integer", default: "error");` doesn't result in a call at runtime to `intFunc()`.
30
28
31
-
When the controlling expression is evaluated, `const`, `volatile`, and `restrict` are removed before matching against *`assoc-list`*.
29
+
When the type of the controlling expression is determined, `const`, `volatile`, and `restrict` are removed before matching against *`assoc-list`*.
32
30
33
31
Entries in the `assoc-list` that aren't chosen aren't evaluated.
0 commit comments