Skip to content

Commit b9e1e84

Browse files
authored
Add C4770 per cpp-docs 4253 (#4649)
* Add C4770 per cpp-docs 4253 * Acrolinx issues * Tweaks to title
1 parent 36b8983 commit b9e1e84

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
description: "Learn more about: Compiler Warning (level 4) C4770"
3+
title: "Compiler Warning (level 4) C4770"
4+
ms.date: 11/02/2022
5+
f1_keywords: ["C4770"]
6+
helpviewer_keywords: ["C4770"]
7+
---
8+
# Compiler Warning (level 4) C4770
9+
10+
> partially validated enum '*symbol*' used as index
11+
12+
The compiler warns if an enum value is cast or aliased to an integer type, but the result isn't checked for non-negative or excessive values.
13+
14+
## Remarks
15+
16+
This warning is new in Visual Studio 2013. It's not enabled by default. To enable it as a level 1 warning, use `/w14770`. For information on how to disable warnings by compiler version, see [Compiler warnings by compiler version](compiler-warnings-by-compiler-version.md).
17+
18+
## Example
19+
20+
The following code produces warning C4770:
21+
22+
```cpp
23+
// c4770.cpp
24+
// compile by using: cl /GL /w14770 c4770.cpp
25+
26+
enum E { a 0, b, c, E_MAX };
27+
28+
int main(int argc, char *argv[])
29+
{
30+
const E e1 = E(argc); // value unknown at compile time
31+
32+
if ((int)(e1) >= E_MAX)
33+
return 0;
34+
35+
const int n = e1 + e1; // C4770 partially validated enum used as index
36+
37+
return argv[n][n];
38+
}
39+
```
40+
41+
To fix the warning, you could cast the value in the check to `unsigned int`, which implicitly forces a non-negative value:
42+
43+
```cpp
44+
if ((unsigned int)(e1) >= E_MAX)
45+
return 0;
46+
```
47+
48+
Or, explicitly check for a non-negative value:
49+
50+
```cpp
51+
if ((int)(e1) >= E_MAX || (int)(e1) < 0)
52+
return 0;
53+
```

docs/error-messages/compiler-warnings/compiler-warnings-c4600-through-c4799.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
description: "Learn more about: Compiler warnings C4600 Through C4799"
33
title: "Compiler warnings C4600 Through C4799"
44
ms.date: 05/03/2021
5-
f1_keywords: ["C4609", "C4658", "C4671", "C4676", "C4689", "C4695", "C4696", "C4719", "C4720", "C4721", "C4728", "C4732", "C4751", "C4752", "C4755", "C4757", "C4767", "C4770"]
6-
helpviewer_keywords: ["C4609", "C4658", "C4671", "C4676", "C4689", "C4695", "C4696", "C4719", "C4720", "C4721", "C4728", "C4732", "C4751", "C4752", "C4755", "C4757", "C4767", "C4770"]
5+
f1_keywords: ["C4609", "C4658", "C4671", "C4676", "C4689", "C4695", "C4696", "C4719", "C4720", "C4721", "C4728", "C4732", "C4751", "C4752", "C4755", "C4757", "C4767"]
6+
helpviewer_keywords: ["C4609", "C4658", "C4671", "C4676", "C4689", "C4695", "C4696", "C4719", "C4720", "C4721", "C4728", "C4732", "C4751", "C4752", "C4755", "C4757", "C4767"]
77
ms.assetid: 22bd4392-f3be-445c-9f23-6126aebac901
88
---
99
# Compiler warnings C4600 Through C4799
@@ -147,7 +147,7 @@ The articles in this section of the documentation explain a subset of the warnin
147147
|[Compiler warning (level 4) C4764](compiler-warning-level-4-c4764.md)|Can not align catch objects to greater than 16 bytes|
148148
|Compiler warning (level 4) C4767|section name '%s' is longer than 8 characters and will be truncated by the linker|
149149
|[Compiler warning (level 3) C4768](c4768.md)|__declspec attributes before linkage specification are ignored|
150-
|Compiler warning C4770|partially validated enum '*name*' used as index|
150+
| [Compiler warning (level 4) C4770](./c4770.md) | partially validated enum '*name*' used as index |
151151
|Compiler warning C4771|Bounds must be created using a simple pointer; MPX intrinsic function ignored|
152152
|[Compiler warning (level 1, Error) C4772](../../error-messages/compiler-warnings/compiler-warning-level-1-c4772.md)|#import referenced a type from a missing type library; 'missing_type' used as a placeholder|
153153
|Compiler warning (level 4) C4774|'*string*' : format string expected in argument *number* is not a string literal|

docs/error-messages/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4156,6 +4156,8 @@ items:
41564156
href: compiler-warnings/compiler-warning-level-4-c4764.md
41574157
- name: Compiler warning (level 4) C4768
41584158
href: compiler-warnings/c4768.md
4159+
- name: Compiler warning (level 4) C4770
4160+
href: compiler-warnings/c4770.md
41594161
- name: Compiler warning (level 1) C4772
41604162
href: compiler-warnings/compiler-warning-level-1-c4772.md
41614163
- name: Compiler warning (Level 1) C4788

0 commit comments

Comments
 (0)