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
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-1/compiler-error-c2440.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ The compiler generates C2440 when it can't convert from one type to another, eit
21
21
22
22
### C++ string literals are `const`
23
23
24
-
C2440 can be caused if you attempt to initialize a non-const `char*` (or `wchar_t*`) by using a string literal in C++ code, when the compiler conformance option [`/Zc:strictStrings`](../../build/reference/zc-strictstrings-disable-string-literal-type-conversion.md) is set. In C, the type of a string literal is array of **`char`**, but in C++, it's array of `const char`. This sample generates C2440:
24
+
C2440 can be caused if you attempt to initialize a non-const `char*` (or `wchar_t*`) by using a string literal in C++ code, when the compiler conformance option [`/Zc:strictStrings`](../../build/reference/zc-strictstrings-disable-string-literal-type-conversion.md) is set. In C, the type of a string literal is array of **`char`**, but in C++, it's array of `const char`. This example generates C2440:
25
25
26
26
```cpp
27
27
// C2440s.cpp
@@ -40,7 +40,7 @@ int main() {
40
40
41
41
### C++20 `u8` literals are `const char8_t`
42
42
43
-
In C++20 or under [`/Zc:char8_t`](../../build/reference/zc-char8-t.md), a UTF-8 literal character or string (such as `u8'a'` or `u8"String"`) is of type `const char8_t` or `const char8_t[N]`, respectively. This sample shows how compiler behavior changes between C++17 and C++20:
43
+
In C++20 or under [`/Zc:char8_t`](../../build/reference/zc-char8-t.md), a UTF-8 literal character or string (such as `u8'a'` or `u8"String"`) is of type `const char8_t` or `const char8_t[N]`, respectively. This example shows how compiler behavior changes between C++17 and C++20:
44
44
45
45
```cpp
46
46
// C2440u8.cpp
@@ -60,7 +60,7 @@ int main() {
60
60
61
61
### Pointer to member
62
62
63
-
You may see C2440 if you attempt to convert a pointer to member to `void*`. The next sample generates C2440:
63
+
You may see C2440 if you attempt to convert a pointer to member to `void*`. The next example generates C2440:
64
64
65
65
```cpp
66
66
// C2440.cpp
@@ -83,7 +83,7 @@ public:
83
83
84
84
### Cast of undefined type
85
85
86
-
The compiler emits C2440 if you attempt to cast from a type that's only forward declared but not defined. This sample generates C2440:
86
+
The compiler emits C2440 if you attempt to cast from a type that's only forward declared but not defined. This example generates C2440:
87
87
88
88
```cpp
89
89
// c2440a.cpp
@@ -98,7 +98,7 @@ Base * func(Derived * d) {
98
98
99
99
### Incompatible calling convention
100
100
101
-
The C2440 errors on lines 15 and 16 of the next sample are qualified with the `Incompatible calling conventions for UDT return value` message. A *UDT* is a user-defined type, such as a class, struct, or union. These kinds of incompatibility errors are caused when the calling convention of a UDT specified in the return type of a forward declaration conflicts with the actual calling convention of the UDT and when a function pointer is involved.
101
+
The C2440 errors on lines 15 and 16 of the next example are qualified with the `Incompatible calling conventions for UDT return value` message. A *UDT* is a user-defined type, such as a class, struct, or union. These kinds of incompatibility errors are caused when the calling convention of a UDT specified in the return type of a forward declaration conflicts with the actual calling convention of the UDT and when a function pointer is involved.
102
102
103
103
In the example, first there are forward declarations for a struct and for a function that returns the struct. The compiler assumes that the struct uses the C++ calling convention. Next is the struct definition, which uses the C calling convention by default. Because the compiler doesn't know the calling convention of the struct until it finishes reading the entire struct, the calling convention for the struct in the return type of `get_c2` is also assumed to be C++.
104
104
@@ -163,7 +163,7 @@ int main() {
163
163
164
164
### User-defined conversions
165
165
166
-
C2440 can also occur for an incorrect use of a user-defined conversion. For example, when a conversion operator has been defined as **`explicit`**, the compiler can't use it in an implicit conversion. For more information about user-defined conversions, see [User-Defined Conversions (C++/CLI)](../../dotnet/user-defined-conversions-cpp-cli.md)). This sample generates C2440:
166
+
C2440 can also occur for an incorrect use of a user-defined conversion. For example, when a conversion operator has been defined as **`explicit`**, the compiler can't use it in an implicit conversion. For more information about user-defined conversions, see [User-Defined Conversions (C++/CLI)](../../dotnet/user-defined-conversions-cpp-cli.md)). This example generates C2440:
167
167
168
168
```cpp
169
169
// C2440d.cpp
@@ -187,7 +187,7 @@ int main() {
187
187
188
188
### `System::Array` creation
189
189
190
-
C2440 can also occur if you try to create an instance of an array in C++/CLI whose type is a <xref:System.Array>. For more information, see [Arrays](../../extensions/arrays-cpp-component-extensions.md). The next sample generates C2440:
190
+
C2440 can also occur if you try to create an instance of an array in C++/CLI whose type is a <xref:System.Array>. For more information, see [Arrays](../../extensions/arrays-cpp-component-extensions.md). The next example generates C2440:
191
191
192
192
```cpp
193
193
// C2440e.cpp
@@ -202,7 +202,7 @@ int main() {
202
202
203
203
### Attributes
204
204
205
-
C2440 can also occur because of changes in the attributes feature. The following sample generates C2440.
205
+
C2440 can also occur because of changes in the attributes feature. The following example generates C2440.
206
206
207
207
```cpp
208
208
// c2440f.cpp
@@ -218,7 +218,7 @@ The Microsoft C++ compiler no longer allows the [`const_cast` operator](../../cp
218
218
219
219
To resolve this C2440, use the correct cast operator. For more information, see [Casting operators](../../cpp/casting-operators.md).
220
220
221
-
This sample generates C2440:
221
+
This example generates C2440:
222
222
223
223
```cpp
224
224
// c2440g.cpp
@@ -237,7 +237,7 @@ int main() {
237
237
238
238
C2440 can occur because of conformance changes to the compiler in Visual Studio 2015 Update 3. Previously, the compiler incorrectly treated certain distinct expressions as the same type when identifying a template match for a **`static_cast`** operation. Now the compiler distinguishes the types correctly, and code that relied on the previous **`static_cast`** behavior is broken. To fix this issue, change the template argument to match the template parameter type, or use a **`reinterpret_cast`** or C-style cast.
0 commit comments