Skip to content

Commit 9981a7e

Browse files
TylerMSFTTylerMSFT
authored andcommitted
edits
1 parent 5a1f247 commit 9981a7e

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

docs/c-language/generic_selection.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
---
22
title: "Generic selection (C11)"
33
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"
55
helpviewer_keywords: ["_Generic keyword [C]"]
66
---
77

88
# Generic selection (C11)
99

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 compile time 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.
1111

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"`.
1513

1614
## Syntax
1715

@@ -26,9 +24,9 @@ For example, the expression `_Generic(42, int: "integer", char: "character", def
2624
    *`type-name`* : *`assignment-expression`*\
2725
    **`default`** : *`assignment-expression`*
2826

29-
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()`.
3028

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`*.
3230

3331
Entries in the `assoc-list` that aren't chosen aren't evaluated.
3432

0 commit comments

Comments
 (0)