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

Pitfalls of inheritence and nested names

I've come across a peculiar case while designing my C++ object inheritance tree in a project. I expected the below to fail: struct A { protected: struct C { }; }; struct B: A { struct C { }; }; But, this compiles just fine. Is this…
Anthony
  • 1,015
  • 8
  • 22
1
vote
2 answers

Why do I get compile time error when base class pointer points to derived class virtual function that is declared in base class?

I have a base class which has virtual void function1( ) and that is overridden in derived class. Additionally there is one more virtual function in my derived class as below. class Base { public: virtual void function1() { …
1
vote
1 answer

It seems to me that there are two candidate functions for the call g(parm, 1) in the example in [basic.lookup.argdep]/3

Example in [basic.lookup.argdep]/3: namespace NS { class T { }; void f(T); void g(T, int); } NS::T parm; void g(NS::T, float); int main() { f(parm); // OK: calls NS::f extern void g(NS::T, float); g(parm, 1); // OK: calls…
1
vote
1 answer

Properly separating common template-function overloads from specialized ones?

Question What is a proper way of having template helpers and additional specialized overloads in separate files so that includes are order-independent? Is there a proper way of doing things in the situations similar to mine (see description…
AMA
  • 4,114
  • 18
  • 32
1
vote
1 answer

How Function Overloading in c++ works without Diamond Inheritance?

In the below Example, void f(double, double); // at global scope struct Grandparent { void f(int); void f(double, double); }; struct Parent : public Grandparent { void f(int); // hides all overloads of Grandparent::f }; struct Child…
Sripooja
  • 49
  • 1
  • 9
1
vote
1 answer

STL algorithm name resolvable or undefined based on template argument deduction utilization

Given this code: #include #include using std::vector; int main() { vector intVec(100, 1); // no problem random_shuffle(intVec.begin(), intVec.end()); // no problem …
schulmaster
  • 413
  • 5
  • 16
1
vote
3 answers

Confusion about output of program with virtual inheritance

I'm new to c++ and have been experimenting with virtual inheritance. But there is something that really confuses me. #include using namespace std; struct A {int m = 5005;}; struct B : A {}; struct C : virtual B {}; struct D : virtual B…
1
vote
0 answers

name-lookup in generic lambdas in intel icc

What are the name-lookup rules in a generic lambdas, if in the surrounding scope names from a different namespace are imported? namespace ns { template void bar2(F f) { f(0); } template void bar1(F f) { bar2(f); …
spraetor
  • 441
  • 4
  • 13
1
vote
1 answer

Why doesn't Ruby find constants defined in the caller's class?

Consider the following code: class MyClass def foo_via_method foo_method end def foo_via_constant FOO_CONSTANT end end class SubClass < MyClass FOO_CONSTANT = "foo" def foo_method FOO_CONSTANT end end The two instance…
hoffm
  • 2,386
  • 23
  • 36
1
vote
0 answers

Friend declaration in namespace: forward declaration or elaborated type specifier

The following code is successfully compiled by Visual C++ 2013 and 2015 however gcc and clang reports a error. I wonder if C++11 standard has something applicable to this case besides the quote below that allows some ambiguity in treatment of friend…
1
vote
1 answer

Finding names in templatized base classes in C++

I'm reading Effective C++ 3rd Edition, item 43 "Know how to access names in templatized base classes". template class B { T i; }; template class D: public B { public: void Foo() { T a = B::i; …
chaosink
  • 1,329
  • 13
  • 27
1
vote
1 answer

Difference between name lookup and name binding in C++

In C++, is there a difference between name binding and name lookup in? The working draft C++14 standard (N4296) defines name lookup in (3.4) as Name lookup associates the use of a name with a declaration (3.1) of that name. I can't find a…
gihan
  • 53
  • 6
1
vote
1 answer

Function names in global namepsace scope cannot be found

According to unqualified lookup in cppreference.com: For a name used in the definition of a function, either in its body or as part of default argument, where the function is a member of user-declared or global namespace, the block in which the…
gnaggnoyil
  • 764
  • 5
  • 14
1
vote
2 answers

Implementing non-member generic function for specific nested class of a class template

I have the following class: template struct A { struct B { auto abs() const { return 1; } }; }; Specifically A is supposed to be the finite field of integers modulo P, and B is the class representing elements from this finite…
1
vote
1 answer

Lookup of base class name after inheriting constructor

Consider the following code: struct base {}; struct derived : public base { using base::base; base foo() const; // how does name lookup on 'base' here work? }; Intuitively, it's clear that this code is valid, and it does compile (tested…