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.

276 questions
188
votes
4 answers

What is "Argument-Dependent Lookup" (aka ADL, or "Koenig Lookup")?

What are some good explanations on what argument dependent lookup is? Many people also call it Koenig Lookup as well. Preferably I'd like to know: Why is it a good thing? Why is it a bad thing? How does it work?
user965369
  • 4,613
  • 10
  • 29
  • 43
86
votes
4 answers

Why doesn't ADL find function templates?

What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile? namespace ns { struct foo {}; …
Hugh
  • 8,562
  • 2
  • 34
  • 42
73
votes
3 answers

Derived template-class access to base-class member-data

This question is a furtherance of the one asked in this thread. Using the following class definitions: template class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg) { /* do something for foo */ } T…
Shamster
  • 1,852
  • 4
  • 21
  • 26
67
votes
7 answers

Propagating 'typedef' from based to derived class for 'template'

I'm trying to define base class, which contains typedef's only. template class A { public: typedef std::vector Vec_t; }; template class B : public A { private: Vec_t v; // fails - Vec_t is not…
dimba
  • 24,103
  • 28
  • 127
  • 188
47
votes
3 answers

The first snippet below compiles, but the second doesn't. Why?

The snippet below compiles (demo): struct A{ int i = 10; }; int main() { struct A{ int i = 20; }; struct A; struct A a; } But this doesn't: struct A{ int i = 10; }; int main() { // struct A{ int i = 20; }; struct A; struct…
João Afonso
  • 1,838
  • 9
  • 19
45
votes
2 answers

What is the fully qualified name of a friend function defined inside of a class?

What is the fully qualified name of a friend function defined inside of a class? I recently saw an example analogous to the following. What is the fully qualified name of val() below? #include namespace foo { class A { int…
Szabolcs
  • 21,860
  • 5
  • 72
  • 158
39
votes
3 answers

Conditional operator's return type and two-phase lookup

Consider the following snippet: struct Base { }; struct Derived : Base { }; void f(Base &) { std::cout << "f(Base&)\n"; } template void g() { Derived d; f(T{} ? d : d); // 1 } void f(Derived &) { std::cout <<…
Quentin
  • 58,778
  • 7
  • 120
  • 175
37
votes
5 answers

GCC issue: using a member of a base class that depends on a template argument

The following code doesn't compile with gcc, but does with Visual Studio: template class A { public: T foo; }; template class B: public A { public: void bar() { cout << foo << endl; } }; I get the…
Jesse Beder
  • 30,017
  • 18
  • 97
  • 140
36
votes
2 answers

In a templated derived class, why do I need to qualify base class member names with "this->" inside a member function?

While I investigate source code of Qt I saw that trolltech guys explicitly use this keyword to access a field on destructor. inline ~QScopedPointer() { T *oldD = this->d; Cleanup::cleanup(oldD); this->d = 0; } So, what's the point of…
useraged
  • 1,616
  • 17
  • 34
34
votes
1 answer

Why does this dependent name lookup find a global identifier instead of the method?

When the compiler tries to resolve i.template hi(); it finds hi in the global namespace instead of the method hi on i (ideone). Why? #include // Define 'hi' and 'bye' in the global namespace; these should *not* be used template
Cameron
  • 86,330
  • 19
  • 177
  • 216
28
votes
2 answers

Two phase name lookup for C++ templates - Why?

Why does the C++ standard define two phase lookup for templates? Couldn't non dependent declarations and definitions' lookups be deferred to the instantiation stage as well?
Xyand
  • 4,290
  • 3
  • 33
  • 59
27
votes
2 answers

Is this-> mandatory to access Base identifiers from derived classes?

This code compiles with MSVC 2015, but doesn't compile with Clang 5.0.0 (trunk 304874): template struct Base { T data; }; template struct Derived : Base { auto getData() const { return data; } }; Replacing…
Mr.C64
  • 37,988
  • 11
  • 76
  • 141
26
votes
2 answers

A weird behavior of using-declaration

please see the following code struct A { using type = int; }; struct B : private A {}; struct C : B { using base_type = A; }; All of gcc 6.1, clang 3.8, and msvc 2015 update 3 refuse to compile this, as A is not an accessible name inside C since A…
25
votes
1 answer

Why does C++'s "using namespace" work the way it does?

All students are surprised by the behavior of C++ using-directives. Consider this snippet (Godbolt): namespace NA { int foo(Zoo::Lion); } namespace NB { int foo(Zoo::Lion); namespace NC { namespace N1 { int…
Quuxplusone
  • 19,419
  • 5
  • 72
  • 137
24
votes
3 answers

Is ADL the only way to call a friend inline function?

Let us define f, as a friend function of S, inside the declaration of S: struct S { friend void f() {} }; I cannot find a way to call f. Is it true, then, that such an inline friend function can only be called with argument-dependant…
YSC
  • 34,418
  • 7
  • 80
  • 129
1
2 3
18 19