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/build/reference/microsoft-extensions-to-c-and-cpp.md
-1Lines changed: 0 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -205,7 +205,6 @@ The C compiler supports the following data declaration and definition features.
205
205
// error C2059: syntax error: 'empty declaration'
206
206
```
207
207
208
-
209
208
## Intrinsic floating-point functions
210
209
211
210
Both the x86 C++ compiler and C compiler support inline generation of the `atan`, `atan2`, `cos`, `exp`, `log`, `log10`, `sin`, `sqrt`, and `tan` functions when **`/Oi`** is specified. These intrinsics don't conform to the standard, because they don't set the `errno` variable.
Copy file name to clipboardExpand all lines: docs/c-runtime-library/reference/atoi-atoi-l-wtoi-wtoi-l.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ These functions convert a character string to an integer value (**`atoi`** and *
53
53
54
54
The *`str`* argument to **`atoi`** and **`_wtoi`** has the following form:
55
55
56
-
> [*`whitespace`*][*`sign`*][*`digits`*]]
56
+
> [*`whitespace`*][*`sign`*][*`digits`*]
57
57
58
58
A *`whitespace`* consists of space or tab characters, which are ignored; *`sign`* is either plus (+) or minus (-); and *`digits`* are one or more digits.
Copy file name to clipboardExpand all lines: docs/c-runtime-library/reference/atol-atol-l-wtol-wtol-l.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ The input string is a sequence of characters that can be interpreted as a numeri
54
54
55
55
The *`str`* argument to **`atol`** has the following form:
56
56
57
-
> [*`whitespace`*][*`sign`*][*`digits`*]]
57
+
> [*`whitespace`*][*`sign`*][*`digits`*]
58
58
59
59
A *`whitespace`* consists of space or tab characters, which are ignored; *`sign`* is either plus (`+`) or minus (`-`); and *`digits`* are one or more digits.
Copy file name to clipboardExpand all lines: docs/cpp/explicitly-defaulted-and-deleted-functions.md
+17-18Lines changed: 17 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,14 @@
2
2
description: "Learn more about: Explicitly Defaulted and Deleted Functions"
3
3
title: "Explicitly Defaulted and Deleted Functions"
4
4
ms.date: "11/04/2016"
5
-
ms.assetid: 5a588478-fda2-4b3f-a279-db3967f5e07e
6
5
---
7
6
# Explicitly Defaulted and Deleted Functions
8
7
9
-
In C++11, defaulted and deleted functions give you explicit control over whether the special member functions are automatically generated. Deleted functions also give you simple language to prevent problematic type promotions from occurring in arguments to functions of all types—special member functions, as well as normal member functions and non-member functions—which would otherwise cause an unwanted function call.
8
+
In C++11, defaulted and deleted functions give you explicit control over whether the special member functions are automatically generated. Deleted functions also give you simple language to prevent problematic type promotions from occurring in arguments to functions of all types—special member functions, and normal member functions and nonmember functions—which would otherwise cause an unwanted function call.
10
9
11
10
## Benefits of explicitly defaulted and deleted functions
12
11
13
-
In C++, the compiler automatically generates the default constructor, copy constructor, copy-assignment operator, and destructor for a type if it does not declare its own. These functions are known as the *special member functions*, and they are what make simple user-defined types in C++ behave like structures do in C. That is, you can create, copy, and destroy them without any additional coding effort. C++11 brings move semantics to the language and adds the move constructor and move-assignment operator to the list of special member functions that the compiler can automatically generate.
12
+
In C++, the compiler automatically generates the default constructor, copy constructor, copy-assignment operator, and destructor for a type if it doesn't declare its own. These functions are known as the *special member functions*, and they're what make simple user-defined types in C++ behave like structures do in C. That is, you can create, copy, and destroy them without extra coding effort. C++11 brings move semantics to the language and adds the move constructor and move-assignment operator to the list of special member functions that the compiler can automatically generate.
14
13
15
14
This is convenient for simple types, but complex types often define one or more of the special member functions themselves, and this can prevent other special member functions from being automatically generated. In practice:
16
15
@@ -36,11 +35,11 @@ This is convenient for simple types, but complex types often define one or more
36
35
> - If a copy constructor or destructor is explicitly declared, then automatic generation of the copy-assignment operator is deprecated.
37
36
> - If a copy-assignment operator or destructor is explicitly declared, then automatic generation of the copy constructor is deprecated.
38
37
>
39
-
> In both cases, Visual Studio continues to automatically generate the necessary functions implicitly, and does not emit a warning.
38
+
> In both cases, Visual Studio continues to automatically generate the necessary functions implicitly, and doesn't emit a warning by default. Since Visual Studio 2022 version 17.7, [C5267](../error-messages/compiler-warnings/c5267.md) can be enabled to emit a warning.
40
39
41
-
The consequences of these rules can also leak into object hierarchies. For example, if for any reason a base class fails to have a default constructor that's callable from a deriving class—that is, a **`public`** or **`protected`** constructor that takes no parameters—then a class that derives from it cannot automatically generate its own default constructor.
40
+
The consequences of these rules can also leak into object hierarchies. For example, if for any reason a base class fails to have a default constructor that's callable from a deriving class—that is, a **`public`** or **`protected`** constructor that takes no parameters—then a class that derives from it can't automatically generate its own default constructor.
42
41
43
-
These rules can complicate the implementation of what should be straight-forward, user-defined types and common C++ idioms—for example, making a user-defined type non-copyable by declaring the copy constructor and copy-assignment operator privately and not defining them.
42
+
These rules can complicate the implementation of what should be straight-forward, user-defined types and common C++ idioms—for example, making a user-defined type noncopyable by declaring the copy constructor and copy-assignment operator privately and not defining them.
44
43
45
44
```cpp
46
45
structnoncopyable
@@ -53,17 +52,17 @@ private:
53
52
};
54
53
```
55
54
56
-
Before C++11, this code snippet was the idiomatic form of non-copyable types. However, it has several problems:
55
+
Before C++11, this code snippet was the idiomatic form of noncopyable types. However, it has several problems:
57
56
58
57
- The copy constructor has to be declared privately to hide it, but because it's declared at all, automatic generation of the default constructor is prevented. You have to explicitly define the default constructor if you want one, even if it does nothing.
59
58
60
-
- Even if the explicitly-defined default constructor does nothing, it's considered non-trivial by the compiler. It's less efficient than an automatically generated default constructor and prevents `noncopyable` from being a true POD type.
59
+
- Even if the explicitlydefined default constructor does nothing, the compiler considers it to be nontrivial. It's less efficient than an automatically generated default constructor and prevents `noncopyable` from being a true POD type.
61
60
62
-
- Even though the copy constructor and copy-assignment operator are hidden from outside code, the member functions and friends of `noncopyable` can still see and call them. If they are declared but not defined, calling them causes a linker error.
61
+
- Even though the copy constructor and copy-assignment operator are hidden from outside code, the member functions and friends of `noncopyable` can still see and call them. If they're declared but not defined, calling them causes a linker error.
63
62
64
-
- Although this is a commonly accepted idiom, the intent is not clear unless you understand all of the rules for automatic generation of the special member functions.
63
+
- Although this is a commonly accepted idiom, the intent isn't clear unless you understand all of the rules for automatic generation of the special member functions.
65
64
66
-
In C++11, the non-copyable idiom can be implemented in a way that is more straightforward.
65
+
In C++11, the noncopyable idiom can be implemented in a way that is more straightforward.
67
66
68
67
```cpp
69
68
struct noncopyable
@@ -78,17 +77,17 @@ Notice how the problems with the pre-C++11 idiom are resolved:
78
77
79
78
- Generation of the default constructor is still prevented by declaring the copy constructor, but you can bring it back by explicitly defaulting it.
80
79
81
-
- Explicitly defaulted special member functions are still considered trivial, so there is no performance penalty, and `noncopyable`is not prevented from being a true POD type.
80
+
- Explicitly defaulted special member functions are still considered trivial, so there's no performance penalty, and `noncopyable`isn't prevented from being a true POD type.
82
81
83
-
- The copy constructor and copy-assignment operator are public but deleted. It is a compile-time error to define or call a deleted function.
82
+
- The copy constructor and copy-assignment operator are public but deleted. It's a compile-time error to define or call a deleted function.
84
83
85
84
- The intent is clear to anyone who understands `=default` and `=delete`. You don't have to understand the rules for automatic generation of special member functions.
86
85
87
-
Similar idioms exist for making user-defined types that are non-movable, that can only be dynamically allocated, or that cannot be dynamically allocated. Each of these idioms have pre-C++11 implementations that suffer similar problems, and that are similarly resolved in C++11 by implementing them in terms of defaulted and deleted special member functions.
86
+
Similar idioms exist for making user-defined types that are nonmovable, that can only be dynamically allocated, or that can't be dynamically allocated. Each of these idioms have pre-C++11 implementations that suffer similar problems, and that are similarly resolved in C++11 by implementing them in terms of defaulted and deleted special member functions.
88
87
89
88
## Explicitly defaulted functions
90
89
91
-
You can default any of the special member functions—to explicitly state that the special member function uses the default implementation, to define the special member function with a non-public access qualifier, or to reinstate a special member function whose automatic generation was prevented by other circumstances.
90
+
You can default any of the special member functions—to explicitly state that the special member function uses the default implementation, to define the special member function with a nonpublic access qualifier, or to reinstate a special member function whose automatic generation was prevented by other circumstances.
92
91
93
92
You default a special member function by declaring it as in this example:
94
93
@@ -109,7 +108,7 @@ Because of the performance benefits of trivial special member functions, we reco
109
108
110
109
## Deleted functions
111
110
112
-
You can delete special member functions as well as normal member functions and non-member functions to prevent them from being defined or called. Deleting of special member functions provides a cleaner way of preventing the compiler from generating special member functions that you don't want. The function must be deleted as it is declared; it cannot be deleted afterwards in the way that a function can be declared and then later defaulted.
111
+
You can delete special member functions and normal member functions and nonmember functions to prevent them from being defined or called. Deleting of special member functions provides a cleaner way of preventing the compiler from generating special member functions that you don't want. The function must be deleted as it's declared; it can't be deleted afterwards in the way that a function can be declared and then later defaulted.
113
112
114
113
```cpp
115
114
structwidget
@@ -119,15 +118,15 @@ struct widget
119
118
};
120
119
```
121
120
122
-
Deleting of normal member function or non-member functions prevents problematic type promotions from causing an unintended function to be called. This works because deleted functions still participate in overload resolution and provide a better match than the function that could be called after the types are promoted. The function call resolves to the more-specific—but deleted—function and causes a compiler error.
121
+
Deleting of normal member function or nonmember functions prevents problematic type promotions from causing an unintended function to be called. This works because deleted functions still participate in overload resolution and provide a better match than the function that could be called after the types are promoted. The function call resolves to the more-specific—but deleted—function and causes a compiler error.
123
122
124
123
```cpp
125
124
// deleted overload prevents call through type promotion of float to double from succeeding.
Notice in the preceding sample that calling `call_with_true_double_only` by using a **`float`** argument would cause a compiler error, but calling `call_with_true_double_only` by using an **`int`** argument would not; in the **`int`** case, the argument will be promoted from **`int`** to **`double`** and successfully call the **`double`** version of the function, even though that might not be what's intended. To ensure that any call to this function by using a non-double argument causes a compiler error, you can declare a template version of the function that's deleted.
129
+
Notice in the preceding sample that calling `call_with_true_double_only` by using a **`float`** argument would cause a compiler error, but calling `call_with_true_double_only` by using an **`int`** argument wouldn't; in the **`int`** case, the argument will be promoted from **`int`** to **`double`** and successfully call the **`double`** version of the function, even though that might not be what you intend. To ensure that any call to this function by using a non-double argument causes a compiler error, you can declare a template version of the deleted function.
This**`__declspec`** attribute tells the compiler that a function does not return. As a consequence, the compiler knows that the code following a call to a **`__declspec(noreturn)`** function is unreachable.
13
+
The**`__declspec`** attribute tells the compiler that a function does not return. The compiler then knows that the code following a call to a **`__declspec(noreturn)`** function is unreachable.
14
14
15
-
If the compiler finds a function with a control path that does not return a value, it generates a warning (C4715) or error message (C2202). If the control path cannot be reached due to a function that never returns, you can use **`__declspec(noreturn)`** to prevent this warning or error.
15
+
If the compiler finds a function with a control path that does not return a value, it generates a warning (C4715) or error message (C2202). If the control path cannot be reached due to a function that never returns, use **`__declspec(noreturn)`** to prevent this warning or error.
16
16
17
17
> [!NOTE]
18
18
> Adding **`__declspec(noreturn)`** to a function that is expected to return can result in undefined behavior.
19
19
20
20
## Example
21
21
22
-
In the following sample,the **`else`** clause does not contain a return statement. Declaring `fatal` as **`__declspec(noreturn)`**avoids an error or warning message.
22
+
In the following example, when the argument for `isZeroOrPositive` is negative, `fatal` is called. There isn't a return statement in that control path, which results in warning C4715 that not all control paths return a value. Declaring `fatal` as **`__declspec(noreturn)`**mitigates that warning, which is desirable because there is no point in it since `fatal()` terminates the program.
0 commit comments