Skip to content

Commit f5a1bc0

Browse files
Merge pull request #4743 from Rageking8/clean-up-c2397
Clean up C2397
2 parents 038ab3e + de37bf1 commit f5a1bc0

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

docs/error-messages/compiler-errors-1/compiler-error-c2397.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,22 @@ The C language allows implicit narrowing conversions in assignments and initiali
1515

1616
A narrowing conversion can be okay when you know the possible range of converted values can fit in the target. In this case, you know more than the compiler does. If you make a narrowing conversion intentionally, make your intentions explicit by using a static cast. Otherwise, this error message almost always indicates you have a bug in your code. You can fix it by making sure the objects you initialize have types that are large enough to handle the inputs.
1717

18-
The following sample generates C2397 and shows one way to fix it:
19-
20-
```
21-
// C2397.cpp -- C++ narrowing conversion diagnostics
22-
// Compile by using: cl /EHsc C2397.cpp
23-
#include <vector>
24-
25-
struct S1 {
26-
int m1;
27-
double m2, m3;
18+
The following sample generates C2397:
19+
20+
```cpp
21+
// C2397.cpp
22+
// compile with: /c
23+
struct S {
24+
int m1;
25+
double m2, m3;
2826
};
2927

30-
void function_C2397(double d1) {
31-
char c1 { 127 }; // OK
32-
char c2 { 513 }; // error C2397
33-
34-
std::vector<S1> vS1;
35-
vS1.push_back({ d1, 2, 3 }); // error C2397
36-
37-
// Possible fix if you know d1 always fits in an int
38-
vS1.push_back({ static_cast<int>(d1), 2, 3 });
28+
void func(double d1) {
29+
char c1 { 127 }; // OK
30+
char c2 { 513 }; // C2397
31+
32+
S arr[2]{};
33+
arr[0] = { d1, 2.0, 3.0 }; // C2397
34+
arr[1] = { static_cast<int>(d1), 2.0, 3.0 }; // OK
3935
}
4036
```

0 commit comments

Comments
 (0)