Skip to content

Commit 1f405db

Browse files
authored
feat: migrate cpp/language/basic_concepts/lookup (#87)
1 parent 67fa887 commit 1f405db

File tree

1 file changed

+68
-0
lines changed
  • src/content/docs/cpp/language/basic_concepts

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: Name lookup
3+
---
4+
5+
import { DR, DRList } from "@components/defect-report";
6+
import { Desc, DescList } from "@components/desc-list";
7+
import { CppHeader } from "@components/header"
8+
import DocLink from "@components/DocLink.astro"
9+
10+
Name lookup is the procedure by which a <DocLink dest="/cpp/language/basic_concepts/name">name</DocLink>, when encountered in a program, is associated with the <DocLink dest="/cpp/language/declarations">declaration</DocLink> that introduced it.
11+
12+
For example, to compile `std::cout << std::endl;`, the compiler performs:
13+
14+
- unqualified name lookup for the name `std`, which finds the declaration of namespace std in the header <CppHeader name="iostream" />
15+
- qualified name lookup for the name `cout`, which finds a variable declaration in the namespace `std`
16+
- qualified name lookup for the name `endl`, which finds a function template declaration in the namespace `std`
17+
- both <DocLink dest="/cpp/language/functions/adl">argument-dependent lookup</DocLink> for the name `operator<<`, which finds multiple function template declarations in the namespace `std`, and qualified name lookup for the name `std::ostream::operator<<`, which finds multiple member function declarations in class <DocLink dest="/cpp/library/io/basic_ostream">`std::ostream`</DocLink>.
18+
19+
For function and function template names, name lookup can associate multiple declarations with the same name, and may obtain additional declarations from <DocLink dest="/cpp/language/functions/adl">argument-dependent lookup</DocLink>. <DocLink dest="/cpp/language/templates/function_template">Template argument deduction</DocLink> may also apply, and the set of declarations is passed to <DocLink dest="/cpp/language/functions/overload_resolution">overload resolution</DocLink>, which selects the declaration that will be used. <DocLink dest="/cpp/language/classes/access">Member access</DocLink> rules, if applicable, are considered only after name lookup and overload resolution.
20+
21+
For all other names (variables, namespaces, classes, etc), name lookup can associate multiple declarations only if they declare the same <DocLink dest="/cpp/language/basic_concepts">entity</DocLink>, otherwise it must produce a single declaration in order for the program to compile. Lookup for a name in a scope finds all declarations of that name, with one exception, known as the "struct hack" or "type/non-type hiding": Within the same scope, some occurrences of a name may refer to a declaration of a class/struct/union/enum that is not a `typedef`, while all other occurrences of the same name either all refer to the same variable, non-static data member, or enumerator, or they all refer to possibly overloaded function or function template names. In this case, there is no error, but the type name is hidden from lookup (the code must use <DocLink dest="/cpp/language/declarations/elaborated_type_specifier">elaborated type specifier</DocLink> to access it).
22+
23+
## Types of lookup
24+
25+
If the name appears immediately to the right of the scope resolution `operator ::` or possibly after `::` followed by the disambiguating keyword `template`, see
26+
27+
- <DocLink dest="/cpp/language/basic_concepts/qualified_lookup">Qualified name lookup</DocLink>
28+
29+
Otherwise, see
30+
31+
- <DocLink dest="/cpp/language/basic_concepts/unqualified_lookup">Unqualified name lookup</DocLink>
32+
- (which, for function names, includes <DocLink dest="/cpp/language/functions/adl">Argument-dependent lookup</DocLink>)
33+
34+
## Defect reports
35+
36+
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
37+
38+
<DRList>
39+
<DR kind="cwg" id={2063} std="C++98">
40+
<Fragment slot="behavior-published">
41+
"struct hack" did not apply in class scope (breaks C compatibility)
42+
</Fragment>
43+
<Fragment slot="correct-behavior">
44+
applied
45+
</Fragment>
46+
</DR>
47+
<DR kind="cwg" id={2218} std="C++98">
48+
<Fragment slot="behavior-published">
49+
lookup for non-function (template) names could not associate multiple declarations, even if they declare the same entity
50+
</Fragment>
51+
<Fragment slot="correct-behavior">
52+
allowed
53+
</Fragment>
54+
</DR>
55+
</DRList>
56+
57+
## See also
58+
59+
- <DocLink dest="/cpp/language/basic_concepts/scope">Scope</DocLink>
60+
- <DocLink dest="/cpp/language/functions/adl">Argument-dependent lookup</DocLink> (ADL)
61+
- <DocLink dest="/cpp/language/templates/function_template" section="template-argument-deduction">Template argument deduction</DocLink>
62+
- <DocLink dest="/cpp/language/functions/overload_resolution">Overload resolution</DocLink>
63+
64+
<DescList>
65+
<Desc>
66+
<DocLink slot="item" dest="/c/language/basic_concepts/name_space"> C documentation</DocLink> for **Lookup and name spaces**
67+
</Desc>
68+
</DescList>

0 commit comments

Comments
 (0)