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

In Ruby, why does `Array.length` give NoMethodError?

In Python, I can get the length of a list by calling its __len__ method. __len__ is not defined on each individual list object, but on the list class. The method can be accessed directly through the class: >>> [].__len__() 0 >>> type([])
user200783
  • 13,722
  • 12
  • 69
  • 135
2
votes
2 answers

Namespace lookup order in C

Is there an order of lookup in the namespaces namely, tag namespace and ordinary name space ? Consider the following code : #include int main (void){ typedef struct{ //This belongs to ordinary name space int min; } st; st…
0decimal0
  • 3,884
  • 2
  • 24
  • 39
2
votes
2 answers

C++ Overloaded function name lookup error

I encountered a strange error regarding name lookup in C++. The error can be recreated using the following minimal example: #include #include std::ostream& operator<<(std::ostream& out, const std::vector& a) { for…
zuenni
  • 23
  • 3
2
votes
2 answers

Name lookup Clarification

$10.2/4- "[ Note: Looking up a name in an elaborated-type-specifier (3.4.4) or base-specifier (Clause 10), for instance, ignores all nontype declarations, while looking up a name in a nested-name-specifier (3.4.3) ignores function,…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
2
votes
1 answer

Who is correct here, GCC or MSVC?

Suppose we have this structure: namespace some_namespace::types { using foo_t = int; } namespace some_namespace::classes { class bar { public: auto do_stuff() -> types::foo_t; }; } using namespace…
Jan Hohenheim
  • 3,552
  • 2
  • 17
  • 42
2
votes
1 answer

Why is the lookup for T in this code successful?

The lookup for T should fail class A { typedef int T; void f(void) { class B {}; class C { class D : public B { void g(void) { T a; …
2
votes
1 answer

Data member access ambiguity and diamond inheritance

Given the code: #include #include int main() { struct A { int i; A(int j) : i(j) { ; } }; struct E { int i = 3; }; struct B : A, E { using A::A; }; struct C : A, E { using A::A; }; struct D : B, C { D(int i,…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
2
votes
3 answers

Name Lookup and class scope

Why is it that the return type of setVal is of type string and the parameter type is of type double typedef string Type; Type initVal(); class Exercise { public: typedef double Type; Type setVal(Type); Type initVal(); private: int…
The Enigma
  • 137
  • 2
  • 6
2
votes
1 answer

C++ name lookup affected by declaration of a template method

I don't understand the need for the static_cast in the following C++ code snippet (tested with GCC-4.7): #include class Interface { public: virtual void g(class C* intf) = 0; virtual ~Interface() {} }; class C { public: void…
2
votes
2 answers

Friend declarations: Is this a bug in clang?

§3.4.1/3 has the following example: typedef int f; namespace N { struct A { friend void f(A &); operator int(); void g(A a) { int i = f(a); } }; } Which compiles without errors (see…
Wake up Brazil
  • 3,421
  • 12
  • 19
2
votes
4 answers

How Name Lookup is applied in Namespaces

I don't understand why the following code gives error: namespace A { void f(double x){cout<<"A::f(double)\n";} void f(string s){cout<<"A::f(string)\n";} namespace B { using namespace A; void f(int…
Rakib
  • 7,435
  • 7
  • 29
  • 45
2
votes
1 answer

What outermost block scope of A::N::f the Standard is talking about in the example at §3.4.1/6

Example at 3.4.1/6 namespace A { namespace N { void f(); } } void A::N::f() { i = 5; // The following scopes are searched for a declaration of i: // 1) outermost block scope of A::N::f, before the use of i // 2) scope of…
Wake up Brazil
  • 3,421
  • 12
  • 19
2
votes
0 answers

How to access protected member variable of template parent base class within diamond inheritence

I have a more complicated version of the following code in a larger project. template class Base{ private: protected: classT var; public: classT getVar(){return var;} void setVar(classT varIn){var =…
2
votes
4 answers

C++ STL fails to find comparator for nested class

I expected this code to work, but it fails to compile with GCC. It does compile if you lift the inner class out. #include template struct Outer { struct Inner { int x; }; Inner vec[3]; }; template
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2
votes
4 answers

Name Lookup inside the parameter list of member function

typedef int abc; class Some{ public: abc foo(){...} typedef double abc; }; In the code above, I get it that I get an error: error: changes meaning of 'abc' from 'typedef int abc' because in the book c++ primer,fifth edtion, it…
longtengaa
  • 300
  • 3
  • 11