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

Function overloading between anonymous namespace and named namespace

Is this not allowed? Can someone please explain why? Algorithms.h namespace Algorithms { int kthLargest(std::vector const& nums, int k); } Algorithms.cpp #include "Algorithms.h" namespace { int kthLargest(std::vector const& nums,…
betelgeuse
  • 79
  • 5
4
votes
1 answer

Overload resolution of a qualified name

Consider this function call: foo::bar(); 11.3.1.1.1, paragraph 3 [over.call.func] (N4778) covers this case: In unqualified function calls, the name is not qualified by an -> or . operator and has the more general form of a primary-expression. The…
4
votes
1 answer

Ambiguous member template lookup

An answer to this question says in the following code: #include using std::vector; struct foo { template void vector(); }; int main() { foo f; f.vector(); // ambiguous! } The last line in main is ambiguous,…
Destructor
  • 14,123
  • 11
  • 61
  • 126
4
votes
1 answer

Difference between lookup rules for friend function defined inside vs outside of the class

The following code: struct X { X() {} }; struct Y { Y() {} Y(X) {} Y(int) {} friend bool operator==(const Y&, const Y&) { return false; } }; bool f() { return 1 == X(); } fails to compile with the following error: error:…
anxieux
  • 757
  • 5
  • 14
4
votes
2 answers

C++ Nested `namespace` `using` Name Lookup Order of Preference

I was reading about using-directives on cppreference.com and they had some code I couldn't figure out the order preference for name lookup. I have read about the the transitive property of using-directives on paragraph 3,…
dosentmatter
  • 1,494
  • 1
  • 16
  • 23
4
votes
3 answers

Do struct tags, union tags and enum tags have separate namespaces?

schot's answer is a good one. He claimed that Tags (names of structures, unions and enumerations). I think that the tags for structures, unions and enumerations have different namespaces, so that this code is completely fine: // In the same…
iBug
  • 35,554
  • 7
  • 89
  • 134
4
votes
4 answers

Why does the size of the same identifier differ in C and C++?

#include int T; int main() { struct T { double x; }; printf("%zu", sizeof(T)); return 0; } If I run this code in C, the result is 4, while in C++ it is 8. Can someone explain why the difference?
developer.ahm
  • 180
  • 10
4
votes
2 answers

Private inheritance, return reference to static member of base class

I have a simple question regarding inheriting from a class which privately inheriting of a base class, i.e. we have class Base {}; class Heir: private Base {}; class HeirsHeir : public Heir {}; In understand that HeirsHeir cannot access anything of…
Chris N
  • 53
  • 7
4
votes
3 answers

c++ ordinary lookup vs argument dependent lookup

Considering this sample described in http://en.cppreference.com/w/cpp/language/adl: namespace A { struct X; struct Y; void f(int); void g(X); } namespace B { void f(int i) { f(i); // calls B::f (endless…
FlashMcQueen
  • 635
  • 3
  • 13
4
votes
3 answers

C++ template class inherit

how to define a template class inherit from template class ? I want to wrap std::queue and std::priority_queue to a base class. In my case is LooperQueue. I use StdQueue in this way auto queue = new StdQueue(). my class define…
Fantasy_RQG
  • 143
  • 1
  • 13
4
votes
2 answers

Overload Resolution/Ambiguity in name lookup(which one)

$7.3.3/14 (C++03) struct A { int x(); }; struct B : A { }; struct C : A { using A::x; int x(int); }; struct D : B, C { using C::x; int x(double); }; int f(D* d) { return d->x(); // ambiguous: B::x or C::x } The comment in the code in…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
4
votes
2 answers

Why are these method calls ambiguous?

#include using String = std::string; class Base { protected: String value; }; class Readonly : virtual Base { public: const String& method() const { return value; } String& method() { return value; …
4
votes
1 answer

Cannot get templates to compile under Visual Studio and clang

I have the following minified code. The line with // Only VS compiles on VS but not on clang, And the line with // Only clang compiles on clang but not on VS. Who is correct? More importantly, how to make an equivalent line compile on both? The…
Daniel
  • 30,896
  • 18
  • 85
  • 139
4
votes
1 answer

How does a using-declaration reduce the names available for lookup without ADL?

#include #include class X {}; namespace N { std::string to_string(X) { return "foo"; } void foo() { //using std::to_string; // will break the build if uncommented... //using N::to_string; // ...unless this is…
gd1
  • 11,300
  • 7
  • 49
  • 88
4
votes
2 answers

Resolving virtual method overloads across base classes

Visual Studio 2013. Given: class base_1 { public: virtual void foo(int) = 0; }; class base_2 { public: virtual void foo(int, double) = 0; }; class join_1_2 : public virtual base_1, public virtual base_2 {}; I have a sink: void…
Matthew Reddington
  • 1,409
  • 3
  • 13
  • 24