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
This rule helps to find places where static casts are used to convert between integral types. These casts are unsafe because the compiler would not warn if any data loss occurs. Brace initializers are better for the cases where constants are used, and a compiler error is desired. There are also utilities from the Guidelines Support Library that help to describe intentions clearly:
18
18
19
-
- gsl::narrow ensures lossless conversion and causes run-time crash if it is not possible.
20
-
- gsl::narrow_cast clearly states that conversion can lose data and it is acceptable.
19
+
-`gsl::narrow` ensures lossless conversion and throws `gsl::narrowing_error`if it's not possible.
20
+
-`gsl::narrow_cast` clearly states that conversion can lose data and it's acceptable.
21
21
22
22
## Remarks
23
23
24
-
- This rule is implemented only for static_casts. Using of C-style casts is generally discouraged.
24
+
- This rule is implemented only for static casts. Using of C-style casts is generally discouraged.
Function-style casts (for example, `int(1.1)`) are another form of C-style casts (like `(int)1.1`), which have questionable safety. Specifically, the compiler doesn't try to check if any data loss can occur either in C-casts or in function casts. In both cases, it's better either to avoid casting or to use a braced initializer if possible. If neither works, static casts may be suitable, but it's still better to use utilities from the Guidelines Support Library:
18
18
19
-
-`gsl::narrow` ensures lossless conversion and causes run-time crash if it's not possible.
19
+
-`gsl::narrow` ensures lossless conversion and throws `gsl::narrowing_error` if it's not possible.
20
20
-`gsl::narrow_cast` clearly states that conversion can lose data and it's acceptable.
0 commit comments