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/name-resolution-for-locally-declared-names.md
+19-12Lines changed: 19 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,12 +31,14 @@ translation.priority.ht:
31
31
- "zh-tw"
32
32
---
33
33
# Name Resolution for Locally Declared Names
34
+
34
35
The template's name itself can be referred to with or without the template arguments. In the scope of a class template, the name itself refers to the template. In the scope of a template specialization or partial specialization, the name alone refers to the specialization or partial specialization. Other specializations or partial specializations of the template can also be referenced, with the appropriate template arguments.
35
36
36
-
## Example
37
+
## Example
38
+
37
39
The following code shows that the class template's name A is interpreted differently in the scope of a specialization or partial specialization.
38
40
39
-
```
41
+
```cpp
40
42
// template_name_resolution3.cpp
41
43
// compile with: /c
42
44
template <classT> classA {
@@ -54,12 +56,13 @@ template<> class A<int> {
54
56
};
55
57
```
56
58
57
-
## Example
59
+
## Example
60
+
58
61
In the case of a name conflict between a template parameter and another object, the template parameter can or cannot be hidden. The following rules will help determine precedence.
59
62
60
63
The template parameter is in scope from the point where it first appears until the end of the class or function template. If the name appears again in the template argument list or in the list of base classes, it refers to the same type. In standard C++, no other name that is identical to the template parameter can be declared in the same scope. A Microsoft extension allows the template parameter to be redefined in the scope of the template. The following example shows using the template parameter in the base specification of a class template.
61
64
62
-
```
65
+
```cpp
63
66
// template_name_resolution4.cpp
64
67
// compile with: /EHsc
65
68
template <class T>
@@ -73,10 +76,11 @@ int main() {
73
76
}
74
77
```
75
78
76
-
## Example
79
+
## Example
80
+
77
81
When defining a template's member functions outside the class template, a different template parameter name can be used. If the template member function definition uses a different name for the template parameter than the declaration does, and the name used in the definition conflicts with another member of the declaration, the member in the template declaration takes precedence.
78
82
79
-
```
83
+
```cpp
80
84
// template_name_resolution5.cpp
81
85
// compile with: /EHsc
82
86
#include<iostream>
@@ -107,10 +111,11 @@ int main() {
107
111
Z::Z()
108
112
```
109
113
110
-
## Example
114
+
## Example
115
+
111
116
When defining a template function or member function outside the namespace in which the template was declared, the template argument takes precedence over the names of other members of the namespace.
112
117
113
-
```
118
+
```cpp
114
119
// template_name_resolution6.cpp
115
120
// compile with: /EHsc
116
121
#include<iostream>
@@ -140,10 +145,11 @@ int main() {
140
145
C<T>::g
141
146
```
142
147
143
-
## Example
148
+
## Example
149
+
144
150
In definitions that are outside of the template class declaration, if a template class has a base class that does not depend on a template argument and if the base class or one of its members has the same name as a template argument, then the base class or member name hides the template argument.
0 commit comments