Skip to content

Commit cd62c85

Browse files
Add alternative solution to code-quality/c33010.md
1 parent 52cc349 commit cd62c85

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/code-quality/c33010.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ void foo(Index idx, PFN(&functions)[5])
7171
}
7272
```
7373

74+
Alternatively, the issue can be fixed by choosing an underlying type for `Index` that is unsigned. Because an unsigned value is always positive, it is sufficient to only check the upper bound.
75+
76+
```cpp
77+
typedef void (*PFN)();
78+
79+
enum class Index : unsigned int
80+
{
81+
Zero,
82+
One,
83+
Two,
84+
Three,
85+
Max
86+
};
87+
88+
void foo(Index idx, PFN(&functions)[5])
89+
{
90+
if (idx > Index::Max)
91+
return;
92+
93+
auto pfn = functions[static_cast<unsigned int>(idx)];
94+
if (pfn != nullptr)
95+
(*pfn)();
96+
}
97+
```
98+
7499
## See also
75100
76101
[C33011](./c33011.md)

0 commit comments

Comments
 (0)