Questions tagged [name-lookup]

Name lookup is the procedure by which a name, when encountered in a program, is associated with the declaration that introduced it.

For function names, name lookup can associate multiple declarations with the same name, and may obtain additional declarations from . Template argument deduction may also apply, and the set of declarations is passed to overload resolution, which selects the declaration that will be used. Member access rules, if applicable, are considered only after name lookup and overload resolution. For all other names (variables, namespaces, classes, etc), name lookup must produce a single declaration in order for the program to compile.

Unqualified name lookup

For an unqualified name, that is name that does not appear to the right of a scope resolution operator ::, name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.

For the purpose of unqualified name lookup, all declarations from a namespace mentioned by a using directive are members of the namespace in which the using-directive appears.

303 questions
4
votes
2 answers

When does overload resolution of non-dependent name take place, in definition context or point of instantiation?

3.4 [basic.lookup]/p1 Overload resolution (13.3) takes place after name lookup has succeeded. void g(long); void g(int, int); template void f() { g(0); } void g(int, int = 0) {} int main(){ f(); } gcc compiles succeed, clang…
4
votes
2 answers

Interpretation of [basic.scope.hiding]p2 when unqualified name lookup involves using-directives

There are two types of name hiding in c++: 1) Normal name hiding: [basic.scope.hiding]p1 (http://eel.is/c++draft/basic.scope.hiding#1): A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived…
Supremum
  • 542
  • 7
  • 23
4
votes
1 answer

Name-lookup ambiguity inconsistency

I'm trying to understand why this program does not give an name-lookup ambiguity for i: namespace X { int i = 1; } namespace Q { namespace P { int i = 2; using namespace X; } using namespace P; int l =…
Supremum
  • 542
  • 7
  • 23
4
votes
0 answers

C++ non-qualified lookup

I have the following code: //mystd plays a role of the std namespace //which does not allow any new overloads in it //so we define one in the global namespace namespace mystd { template struct A {}; } //if one uncomment this,…
4
votes
1 answer

I'd like to see an example of a function name being ignored in a nested-name-specifier

Foot note (33) in page 53 of N4140: Lookups in which function names are ignored include names appearing in a nested-name-specifier, an elaborated-type-specifier, or a base-specifier.
Ayrosa
  • 3,385
  • 1
  • 18
  • 29
4
votes
3 answers

I don't understand 3.4/2 in the Standard

I don't understand 3.4/2 in the Standard: A name “looked up in the context of an expression” is looked up as an unqualified name in the scope where the expression is found. What if the name is qualified, as N::i below? #include…
Mao
  • 1,065
  • 8
  • 12
4
votes
3 answers

How to name a nested template in a templated base class?

In the following setup, how can I make it so that I can refer to the name Bar inside the derived class Derived? template struct Foo { template struct Bar { }; }; template struct Derived : Foo { …
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3
votes
2 answers

Confusion understanding Virtual function call and dependent base class

I am reading from ebook Templates complete guide and question which i'm gonna ask might be stupid to you but.. There is section in that 9.4.2 Dependent Base Classes which i am unable to understand. Here is the partial text from it:…
Mr.Anubis
  • 5,132
  • 6
  • 29
  • 44
3
votes
2 answers

Do constructors of class T hide methods with the same name T in base class?

I'm trying to figure out why major compilers refuse to compile the following example (godbolt): struct Base { void Derived(); }; struct Derived : public Base { }; int main() { Derived x; x.Derived(); // Compilation error. …
Igor G
  • 1,838
  • 6
  • 18
3
votes
3 answers

Why do I get an error in this code when using "using namespace std;" and "bits/stdc++.h"?

Actually this code works fine in "DEV C++", but when I put it into my "Hacker-Rank" panel it gives this error "reference to function is ambiguous", although all the online compilers are giving errors... I don't think here function overloading is…
Pranshu_Taneja
  • 186
  • 1
  • 1
  • 16
3
votes
1 answer

Overload resolution in nested namespace with parent namespace

I thought that in a nested namespace, anything that is part of the parent (or global) namespace is considered equally for overload resolution, but this example seems to show otherwise. This works fine: #include void foo(int) { std::cout…
Baruch
  • 20,590
  • 28
  • 126
  • 201
3
votes
1 answer

Ordinary lookup and hiding

The following program compiles (live demo), but I don't understand, why. namespace N { struct S {}; } void Foo(N::S); namespace Lib { template void Call() { Foo(T{}); } void Foo(); } int main() { …
Dr. Gut
  • 2,053
  • 7
  • 26
3
votes
1 answer

ADL name lookup problem, is using std::swap; swap(a,b) related to function overloading or inner scope function hide outer scope function?

I know what ADL is and I know in C++, inner scope function hides outer scope functions. That is, names do not overload across scopes. So funtion overloading need to be done in the same scope. So now my question is, for this common code…
Rick
  • 7,007
  • 2
  • 49
  • 79
3
votes
2 answers

Is it possible to have a non-friend function which can only be found by ADL?

C++ has a feature, that in-class-defined friend functions can only be found by ADL (argument dependent lookup): struct Foo { friend void fn(Foo) { } // fn can only be called by ADL, it won't be found by other lookup methods }; Is it possible to…
geza
  • 28,403
  • 6
  • 61
  • 135
3
votes
2 answers

What the C++ standard exactly means by "same scope" in [basic.scope.hiding]?

In C++14 standard, [basic.scope.hiding], paragraph 2 (3.3.10.2), says: A class name or enumeration name can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope. If a class or enumeration name and a…
Pierre
  • 1,942
  • 3
  • 23
  • 43