Skip to content

Commit 3aa134c

Browse files
authored
Merge pull request MicrosoftDocs#5071 from MicrosoftDocs/main
10/18/2023 AM Publish
2 parents c4eaa46 + 6e7bba4 commit 3aa134c

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
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
```

docs/error-messages/compiler-errors-2/compiler-error-c2510.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,18 @@ ms.assetid: bf6d28db-f2f4-48f8-8f4e-7d662ed278fe
1111
'identifier' : left of '::' must be a class/struct/union
1212

1313
A class, structure, or union name must appear on the left side of the scope-resolution operator (`::`) operator.
14+
15+
The following sample generates C2510:
16+
17+
```cpp
18+
// C2510.cpp
19+
struct S {
20+
static const int x = 1;
21+
};
22+
23+
int main() {
24+
S s;
25+
int num1 = s::x; // C2510
26+
int num2 = S::x; // OK
27+
}
28+
```
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
---
22
description: "Learn more about: Compiler Error C3551"
33
title: "Compiler Error C3551"
4-
ms.date: "11/04/2016"
4+
ms.date: "10/07/2023"
55
f1_keywords: ["C3551"]
66
helpviewer_keywords: ["C3551"]
77
ms.assetid: c8ee23da-6568-40db-93a6-3ddb7ac47712
88
---
99
# Compiler Error C3551
1010

11-
"expected a late specified return type"
11+
if a trailing return type is used then the leading return type shall be the single type-specifier 'auto' (not 'type')
1212

13-
If you use the **`auto`** keyword as a placeholder for the return type of a function, you must provide a late-specified return type. In the following example, the late-specified return type of function `myFunction` is a pointer to an array of four elements of type **`int`**.
13+
The leading return type in [trailing return type](../../cpp/functions-cpp.md#trailing-return-types) syntax must contain only `auto`.
1414

15-
```
16-
auto myFunction()->int(*)[4];
17-
```
15+
```cpp
16+
// C3551.cpp
17+
// compile with: /c
18+
const auto func1() -> const int; // C3551
19+
auto* func2() -> int*; // C3551
20+
auto& func3() -> int&; // C3551
21+
auto&& func4() -> int&&; // C3551
22+
decltype(auto) func5() -> int; // C3551
1823

19-
## See also
20-
21-
[auto](../../cpp/auto-cpp.md)
24+
auto func6() -> int; // OK
25+
```

docs/error-messages/compiler-warnings/compiler-warning-level-3-c4310.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
---
2-
description: "Learn more about: Compiler Warning (level 3) C4310"
2+
description: "Learn more about: Compiler Warning (level 4) C4310"
33
title: "Compiler Warning (level 3) C4310"
4-
ms.date: "11/04/2016"
4+
ms.date: 10/17/2023
55
f1_keywords: ["C4310"]
66
helpviewer_keywords: ["C4310"]
7-
ms.assetid: cba3eca1-f1ed-499c-9243-337446bdbdd8
87
---
9-
# Compiler Warning (level 3) C4310
8+
# Compiler Warning (level 4) C4310
109

1110
cast truncates constant value
1211

@@ -15,7 +14,8 @@ A constant value is cast to a smaller type. The compiler performs the cast, whic
1514
```cpp
1615
// C4310.cpp
1716
// compile with: /W4
18-
int main() {
17+
int main()
18+
{
1919
long int a;
2020
a = (char) 128; // C4310, use value 0-127 to resolve
2121
}

docs/error-messages/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3606,7 +3606,7 @@ items:
36063606
href: compiler-warnings/compiler-warning-level-2-c4308.md
36073607
- name: Compiler warning (level 2) C4309
36083608
href: compiler-warnings/compiler-warning-level-2-c4309.md
3609-
- name: Compiler warning (level 3) C4310
3609+
- name: Compiler warning (level 4) C4310
36103610
href: compiler-warnings/compiler-warning-level-3-c4310.md
36113611
- name: Compiler warning (level 1) C4311
36123612
href: compiler-warnings/compiler-warning-level-1-c4311.md

0 commit comments

Comments
 (0)