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
7
votes
1 answer

If 'C' inherits from 'B' publicly, B inherits from 'A' privately, Why can't I create an object of 'A' inside 'C'?

I'm using Visual C++, If I compile this code: class A {}; class B : private A {}; class C : public B { void func() { A a{}; } }; I get this error: error C2247: 'A' not accessible because 'B' uses 'private' to inherit from…
StackExchange123
  • 1,871
  • 9
  • 24
7
votes
1 answer

Unqualified name lookup: Why local declaration hides declaration from using directive

Consider this code: namespace A { int i = 24; } namespace B { using namespace A; int i = 11; int k = i; // finds B::i, no ambiguity } And basic.lookup.unqual.2: §6.4.1 Unqualified name lookup [basic.lookup.unqual] The…
bolov
  • 72,283
  • 15
  • 145
  • 224
7
votes
1 answer

Special behavior for decltype of call operator for incomplete types

I've been struggling with a compilation issue, and have been able to shrink the problem down to a small code segment. To set the stage, I'm trying to do CRTP, where the base method calls another in the derived class. The complication is, I want to…
7
votes
2 answers

Multiple Inheritance Template Class

class messageA { }; class messageB { }; template class queue { public: virtual ~queue() {} void submit(T& x) {} }; class A : public queue, public queue { }; int main() { A aa; aa.submit(messageA()); …
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
7
votes
1 answer

name lookup for typedef is buggy in GNU compiler?

The following code #include typedef double A; // a global typedef template struct B // a template class... { A i{22.2}; // global typedef is in scope typedef int A; // now a local typedef with the same name is…
GSi
  • 649
  • 3
  • 10
7
votes
3 answers

Erroneous private base class inaccessible?

Compiling this code using g++ 4.2.1: struct S { }; template struct ST { }; template class ref_count : private BaseType { }; template class rep_base : public RefCountType { }; class wrap_rep :…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
7
votes
2 answers

Name resolution of functions inside templates instantiated with qualified types

Consider the following C++ code example: namespace n { struct A {}; } struct B {}; void foo(int) {} template void quux() { foo(T()); } void foo(n::A) {} void foo(B) {} int main() { quux(); // Error (but works if…
Nikolaus Demmel
  • 379
  • 1
  • 3
  • 11
6
votes
1 answer

Scope resolution for template instantiation

I have the following set of classes (a minimal replication of my real situation): namespace Parent { class A {}; namespace Nested { class A {}; } template class B { A myA; }; } I would expect that the member…
atkins
  • 1,963
  • 14
  • 27
6
votes
1 answer

Unqualified lookup of operators in standard library templates

namespace N { struct A {}; template constexpr bool operator<(const T&, const T&) { return true; } } constexpr bool operator<(const N::A&, const N::A&) { return false; } #include int main() { …
user17732522
  • 53,019
  • 2
  • 56
  • 105
6
votes
2 answers

How does ADL affect this piece of C++ code?

Actually, the below code can not be compiled with Clang using this command: clang++ -std=c++11 test.cc -o test. I just want to mimic the same behavior as "swapping idiom" in C++ to use "using-directive" to enable ADL. But where am I wrong with the…
Jason Yu
  • 1,886
  • 16
  • 26
6
votes
0 answers

When is there an UB because the best overload match was not found by ADL at the point of instantiation?

When a function body is instantiated, dependent function call overload resolution should find the best match in associated namespace through ADL, otherwise the behavior is undefined, [temp.dep.candidate]§1 If the call would be ill-formed or would…
Oliv
  • 17,610
  • 1
  • 29
  • 72
6
votes
1 answer

A question about name lookup with friend function

I have read the standard section of [basic.lookup.unqual] and I am confused about this: typedef int f; namespace N { struct A { friend void f(A &); operator int(); void g(A a) { int i = f(a); // f is the typedef, not the…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
6
votes
2 answers

Should this function call be ambiguous?

I stumbled on this the other day and can't figure out which answer is correct, or whether both are acceptable. Specifically, I'm referring to the call to bar(T{}) in OtherFunction. From what I've been able to test on compiler explorer, the decision…
6
votes
2 answers

Why can't a function in a namespace see my operator<< defined globally?

I've defined an operator<< output function for std::pair instances, for use by some unit tests that want to print the values if they don't watch what's expected. My test code also has pairs that are held as members of another class that has its own…
Wyzard
  • 33,849
  • 3
  • 67
  • 87
6
votes
2 answers

Variable name same as function name giving compiler error... Why?

Ran into an interesting issue today and am trying to understand why. Consider the following: class Base { public: Base(){} ~Base(){} static void function1(){} void function2() { int function1; …