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/cpp/namespaces-cpp.md
+18-20Lines changed: 18 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,11 +39,11 @@ translation.priority.ht:
39
39
- "zh-tw"
40
40
---
41
41
# Namespaces (C++)
42
-
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. All identifiers at namespace scope are visible to one another without qualification. Identifiers outside the namespace can access the members by using the fully qualified name for each identifier, for example `std::vector<std::string> vec;`, or else by a [using Declaration](../cpp/using-declaration.md) for a single identifier (`using std::string`), or a [using Directive](../misc/using-directive-cpp.md) for all the identifiers in the namespace (`using namespace std;`). Code in header files should always use the fully qualified namespace name.
42
+
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. All identifiers at namespace scope are visible to one another without qualification. Identifiers outside the namespace can access the members by using the fully qualified name for each identifier, for example `std::vector<std::string> vec;`, or else by a [using Declaration](../cpp/using-declaration.md) for a single identifier (`using std::string`), or a [using Directive](../cpp/namespaces-cpp.md#using_directives) for all the identifiers in the namespace (`using namespace std;`). Code in header files should always use the fully qualified namespace name.
43
43
44
44
The following example shows a namespace declaration and three ways that code outside the namespace can accesses their members.
45
45
46
-
```
46
+
```cpp
47
47
namespaceContosoData
48
48
{
49
49
class ObjectManager
@@ -57,15 +57,15 @@ namespace ContosoData
57
57
58
58
Use the fully qualified name:
59
59
60
-
```
60
+
```cpp
61
61
ContosoData::ObjectManager mgr;
62
62
mgr.DoSomething();
63
63
ContosoData::Func(mgr);
64
64
```
65
65
66
66
Use a using declaration to bring one identifier into scope:
67
67
68
-
```
68
+
```cpp
69
69
using WidgetsUnlimited::ObjectManager;
70
70
ObjectManager mgr;
71
71
mgr.DoSomething();
@@ -74,15 +74,15 @@ mgr.DoSomething();
74
74
75
75
Use a using directive to bring everything in the namespace into scope:
76
76
77
-
```
77
+
```cpp
78
78
usingnamespaceWidgetsUnlimited;
79
79
ObjectManager mgr;
80
80
mgr.DoSomething();
81
81
Func(mgr);
82
82
83
83
```
84
84
85
-
## using directives
85
+
## <a id="using_directives"></a> using directives
86
86
The `using` directive allows all the names in a **namespace** to be used without the *namespace-name* as an explicit qualifier. Use a using directive in an implementation file (i.e. *.cpp) if you are using several different identifiers in a namespace; if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace. If a local variable has the same name as a namespace variable, the namespace variable is hidden. It is an error to have a namespace variable with the same name as a global variable.
87
87
88
88
> [!NOTE]
@@ -93,7 +93,7 @@ Func(mgr);
93
93
## Declaring namespaces and namespace members
94
94
Typically, you declare a namespace in a header file. If your function implementations are in a separate file, then qualify the function names, as in this example.
95
95
96
-
```
96
+
```cpp
97
97
//contosoData.h
98
98
#pragma once
99
99
namespace ContosoDataServer
@@ -106,7 +106,7 @@ namespace ContosoDataServer
106
106
107
107
Function implementations in contosodata.cpp should use the fully qualified name, even if you place a using directive at the top of the file:
108
108
109
-
```
109
+
```cpp
110
110
#include"contosodata.h"
111
111
usingnamespaceContosoDataServer;
112
112
@@ -123,7 +123,7 @@ int ContosoDataServer::Bar(){return 0;}
123
123
124
124
Members of a named namespace can be defined outside the namespace in which they are declared by explicit qualification of the name being defined. However, the definition must appear after the point of declaration in a namespace that encloses the declaration's namespace. For example:
125
125
126
-
```
126
+
```cpp
127
127
// defining_namespace_members.cpp
128
128
// C2039 expected
129
129
namespace V {
@@ -145,12 +145,12 @@ namespace V {
145
145
If an identifier is not declared in an explicit namespace, it is part of the implicit global namespace. In general, try to avoid making declarations at global scope when possible, except for the entry point [main Function](../c-language/main-function-and-program-execution.md), which is required to be in the global namespace. To explicitly qualify a global identifier, use the scope resolution operator with no name, as in `::SomeFunction(x);`. This will differentiate the identifier from anything with the same name in any other namespace, and it will also help to make your code easier for others to understand.
146
146
147
147
## The std namespace
148
-
All C++ standard library types and functions are declared in the std namespace or namespaces nested inside std.
148
+
All C++ standard library types and functions are declared in the `std` namespace or namespaces nested inside `std`.
149
149
150
150
## Nested namespaces
151
151
Namespaces may be nested. An ordinary nested namespace has unqualified access to its parent’s members, but the parent members do not have unqualified access to the nested namespace (unless it is declared as inline), as shown in the following example:
152
152
153
-
```
153
+
```cpp
154
154
namespaceContosoDataServer
155
155
{
156
156
void Foo();
@@ -172,7 +172,7 @@ namespace ContosoDataServer
172
172
## Inline namespaces (C++ 11)
173
173
In contrast to an ordinary nested namespace, members of an inline namespace are treated as members of the parent namespace. This characteristic enables argument dependent lookup on overloaded functions to work on functions that have overloads in a parent and a nested inline namespace. It also enables you to declare a specialization in a parent namespace for a template that is declared in the inline namespace. The following example shows how external code binds to the inline namespace by default:
174
174
175
-
```
175
+
```cpp
176
176
//Header.h
177
177
#include<string>
178
178
@@ -206,7 +206,7 @@ int main()
206
206
207
207
The following example shows how you can declare a specialization in a parent of a template that is declared in an inline namespace:
208
208
209
-
```
209
+
```cpp
210
210
namespaceParent
211
211
{
212
212
inline namespace new_ns
@@ -229,7 +229,7 @@ namespace Parent
229
229
230
230
The following example shows two versions of an interface, each in a nested namespace. The `v_20` namespace has some modification from the `v_10` interface and is marked as inline. Client code that uses the new library and calls `Contoso::Funcs::Add` will invoke the v_20 version. Code that attempts to call `Contoso::Funcs::Divide` will now get a compile time error. If they really need that function, they can still access the `v_10` version by explicitly calling `Contoso::v_10::Funcs::Divide`.
Namespace names need to be unique, which means that often they should not be too short. If the length of a name makes code difficult to read, or is tedious to type in a header file where using directives can’t be used, then you can make a namespace alias which serves as an abbreviation for the actual name. For example:
269
269
270
-
```
270
+
```cpp
271
271
namespace a_very_long_namespace_name { class Foo {}; }
272
272
namespace AVLNN = a_very_long_namespace_name;
273
273
void Bar(AVLNN::Foo foo){ }
@@ -277,7 +277,7 @@ void Bar(AVLNN::Foo foo){ }
277
277
## anonymous or unnamed namespaces
278
278
You can create an explicit namespace but not give it a name:
279
279
280
-
```
280
+
```cpp
281
281
namespace
282
282
{
283
283
int MyFunc(){}
@@ -286,7 +286,5 @@ namespace
286
286
287
287
This is called an unnamed or anonymous namespace and it is useful when you want to make variable declarations invisible to code in other files (i.e. give them internal linkage) without having to create a named namespace. All code in the same file can see the identifiers in an unnamed namespace but the identifiers, along with the namespace itself, are not visible outside that file—or more precisely outside the translation unit.
288
288
289
-
## Remarks
290
-
291
289
## See Also
292
-
[Declarations and Definitions](declarations-and-definitions-cpp.md)
290
+
[Declarations and Definitions](declarations-and-definitions-cpp.md)
Copy file name to clipboardExpand all lines: docs/cpp/new-and-delete-operators.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
@@ -47,10 +47,10 @@ C++ supports dynamic allocation and deallocation of objects using the [new](../c
47
47
48
48
For a list of the library files that comprise the C Runtime Library and the Standard C++ Library, see [CRT Library Features](../c-runtime-library/crt-library-features.md).
49
49
50
-
## The new operator
50
+
## <aid="new_operator"> </a> The new operator
51
51
When a statement such as the following is encountered in a program, it translates into a call to the function `operator new`:
52
52
53
-
```
53
+
```cpp
54
54
char *pch = newchar[BUFFER_SIZE];
55
55
```
56
56
@@ -74,7 +74,7 @@ The two scopes for `operator new` functions are described in the following table
74
74
75
75
An **operator new** function defined for a class is a static member function (which cannot, therefore, be virtual) that hides the global **operator new** function for objects of that class type. Consider the case where **new** is used to allocate and set memory to a given value:
76
76
77
-
```
77
+
```cpp
78
78
// spec1_the_operator_new_function1.cpp
79
79
#include<malloc.h>
80
80
#include<memory.h>
@@ -104,15 +104,15 @@ int main()
104
104
105
105
The argument supplied in parentheses to **new** is passed to `Blanks::operator new` as the `chInit` argument. However, the global **operator new** function is hidden, causing code such as the following to generate an error:
106
106
107
-
```
107
+
```cpp
108
108
Blanks *SomeBlanks = new Blanks;
109
109
```
110
110
111
111
In Visual C++ 5.0 and earlier, nonclass types and all arrays (regardless of whether they were of **class** type) allocated using the **new** operator always used the global **operator new** function.
112
112
113
113
Beginning with Visual C++ 5.0, the compiler supports member array **new** and **delete** operators in a class declaration. For example:
114
114
115
-
```
115
+
```cpp
116
116
// spec1_the_operator_new_function2.cpp
117
117
classMyClass
118
118
{
@@ -136,7 +136,7 @@ int main()
136
136
### Handling insufficient memory
137
137
Testing for failed memory allocation can be done with code such as the following:
138
138
139
-
```
139
+
```cpp
140
140
// insufficient_memory_conditions.cpp
141
141
// compile with: /EHsc
142
142
#include<iostream>
@@ -153,14 +153,14 @@ int main() {
153
153
154
154
There is another ways to handle failed memory allocation requests: write a custom recovery routine to handle such a failure, then register your function by calling the [_set_new_handler](../c-runtime-library/reference/set-new-handler.md) run-time function.
155
155
156
-
## The delete operator
156
+
## <a id="delete_operator"> </a> The delete operator
157
157
Memory that is dynamically allocated using the **new** operator can be freed using the **delete** operator. The delete operator calls the **operator delete** function, which frees memory back to the available pool. Using the **delete** operator also causes the class destructor (if there is one) to be called.
158
158
159
159
There are global and class-scoped **operator delete** functions. Only one **operator delete** function can be defined for a given class; if defined, it hides the global **operator delete** function. The global **operator delete** function is always called for arrays of any type.
160
160
161
161
The global **operator delete** function. Two forms exist for the global **operator delete** and class-member **operator delete** functions:
Use the `__sptr` and `__uptr` modifiers with pointer declarations. Use the modifiers in the position of a [pointer type qualifier](../c-language/pointer-declarations.md), which means the modifier must follow the asterisk. You cannot use the modifiers with [pointers to members](../cpp/pointers-to-members.md). The modifiers do not affect non-pointer declarations.
58
58
59
-
If you do not use the `__sptr` or `__uptr` modifier, and you enable [Compiler Warning (level 2) C4826](../error-messages/compiler-warnings/compiler-warning-level-2-c4826.md), the compiler issues a warning when a 32-bit pointer is converted to 64 bits.
60
-
61
59
## Example
62
60
The following example declares 32-bit pointers that use the `__sptr` and `__uptr` modifiers, assigns each 32-bit pointer to a 64-bit pointer variable, and then displays the hexadecimal value of each 64-bit pointer. The example is compiled with the native 64-bit compiler and is executed on a 64-bit platform.
Copy file name to clipboardExpand all lines: docs/cpp/templates-cpp.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,7 @@ int i = minimum(a, b);
77
77
78
78
The rules for how the compiler performs type deduction in function templates are based on the rules for ordinary functions. For more information, see [Overload Resolution of Function Template Calls](../cpp/overload-resolution-of-function-template-calls.md).
79
79
80
-
## Type parameters
80
+
## <aid="type_parameters"></a> Type parameters
81
81
In the `minimum` template above, note that the type parameter `T` is not qualified in any way until it is used in the function call parameters, where the const and reference qualifiers are added.
82
82
83
83
There is no practical limit to the number of type parameters. Separate multiple parameters by commas:
@@ -160,7 +160,7 @@ MyArray<MyClass*, 10> arr;
160
160
161
161
Other kinds of values including pointers and references can be passed in as non-type parameters. For example, you can pass in a pointer to a function or function object to customize some operation inside the template code.
162
162
163
-
## Templates as template parameters
163
+
## <aid="template_parameters"></a> Templates as template parameters
164
164
A template can be a template parameter. In this example, MyClass2 has two template parameters: a typename parameter `T` and a template parameter `Arr`:
Copy file name to clipboardExpand all lines: docs/cpp/unions.md
+9-11Lines changed: 9 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,9 +43,8 @@ A `union` is a user-defined type in which all members share the same memory loca
43
43
44
44
## Syntax
45
45
46
-
```
46
+
```cpp
47
47
union [name] { member-list };
48
-
49
48
```
50
49
51
50
#### Parameters
@@ -60,7 +59,7 @@ union [name] { member-list };
60
59
## Declaring a Union
61
60
Begin the declaration of a union with the `union` keyword, and enclose the member list in curly braces:
62
61
63
-
```
62
+
```cpp
64
63
// declaring_a_union.cpp
65
64
union RecordType // Declare a simple union type
66
65
{
@@ -82,7 +81,7 @@ int main()
82
81
## Using unions
83
82
In the previous example, any code that accesses the union needs to know which member is holding the data. The most common solution to this problem is to enclose the union in a struct along with an additional enum member that indicates the type of the data currently being stored in the union. This is called a *discriminated union* and the following example shows the basic pattern.
84
83
85
-
```
84
+
```cpp
86
85
87
86
#include"stdafx.h"
88
87
#include<queue>
@@ -175,7 +174,7 @@ void Initialize()
175
174
## Unrestricted Unions (C++11)
176
175
In C++03 and earlier a union can contain non-static data members with class type as long as the type has no user provided constructors, destructors or assignment operators. In C++11, these restrictions are removed. If you include such a member in your union then the compiler will automatically mark any special member functions that are not user provided as deleted. If the union is an anonymous union inside a class or struct, then any special member functions of the class or struct that are not user provided are marked as deleted. The following example shows how to handle the case where one of the members of the union has a member that requires this special treatment:
177
176
178
-
```
177
+
```cpp
179
178
// for MyVariant
180
179
#include <crtdbg.h>
181
180
#include <new>
@@ -625,7 +624,7 @@ private:
625
624
## Initializing unions
626
625
You can declare and initialize a union in the same statement by assigning an expression enclosed in braces. The expression is evaluated and assigned to the first field of the union.
627
626
628
-
```
627
+
```cpp
629
628
#include<iostream>
630
629
usingnamespacestd;
631
630
@@ -655,17 +654,16 @@ int main()
655
654

656
655
Storage of Data in NumericType Union
657
656
658
-
## Anonymous unions
657
+
## <aid="anonymous_unions"> </a> Anonymous unions
659
658
Anonymous unions are unions that are declared without a *class-name* or *declarator-list*.
660
659
661
-
```
662
-
660
+
```cpp
663
661
union { member-list }
664
662
```
665
663
666
-
Names declared in an anonymous union are used directly, like nonmember variables. Therefore, the names declared in an anonymous union must be unique in the surrounding scope.
664
+
Names declared in an anonymous union are used directly, like nonmember variables. Therefore, the names declared in an anonymous union must be unique in the surrounding scope.
667
665
668
-
In addition to the restrictions for named unions, anonymous unions are subject to additional restrictions:
666
+
In addition to the restrictions for named unions, anonymous unions are subject to these additional restrictions:
669
667
670
668
- They must also be declared as **static** if declared in file or namespace scope.
Copy file name to clipboardExpand all lines: docs/cpp/using-declaration.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
@@ -50,7 +50,7 @@ using :: unqualified-id
50
50
```
51
51
52
52
## Remarks
53
-
The name becomes a synonym for an entity declared elsewhere. It allows an *individual* name from a specific namespace to be used without [explicit qualification](../misc/explicit-qualification.md). This is in contrast to the `using` directive, which allows *all* the names in a namespace to be used without qualification. See [using Directive](../misc/using-directive-cpp.md) for more information. This keyword is also used for [type aliases](../cpp/aliases-and-typedefs-cpp.md).
53
+
The name becomes a synonym for an entity declared elsewhere. It allows an *individual* name from a specific namespace to be used without explicit qualification. This is in contrast to the `using` directive, which allows *all* the names in a namespace to be used without qualification. See [using directives](../cpp/namespaces-cpp.md#using_directives) for more information. This keyword is also used for [type aliases](../cpp/aliases-and-typedefs-cpp.md).
54
54
55
55
## Example
56
56
A using declaration can be used in a class definition.
0 commit comments