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

Template dependent name resolution should not find declarations with no linkage?

In the c++ standard [temp.point] it is written: The instantiation context of an expression that depends on the template arguments is the set of declarations with external linkage declared prior to the point of instantiation of the template …
Oliv
  • 17,610
  • 1
  • 29
  • 72
9
votes
2 answers

Can't understand name lookup differences between an int and a user defined type - perhaps ADL related

Why does the following code compile: template void foo(T in) { bar(in); } struct type{}; void bar(type) {} int main() { foo(type()); } When the following does not: template void foo(T in) { bar(in); } void bar(int) {} int…
9
votes
4 answers

Does overriding a non-const virtual method hide a const overload?

Consider: #include using namespace std; struct A { virtual void f() { cout << "A::f" << endl; } virtual void f() const { cout << "A::f const" << endl; } }; struct B : public A {}; struct C : public A { virtual void f() { cout…
Ari
  • 3,460
  • 3
  • 24
  • 31
9
votes
2 answers

Compiler discrepancy: Interaction between alias resolution and name lookup

Consider this code: using type = long; namespace n { using type = long; } using namespace n; int main() { type t; } This compiles cleanly on Clang 3.7 and GCC 5.3, but MSVC 19* gives the following error message: main.cpp(9): error C2872:…
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
9
votes
1 answer

Why is a program rejected as ambiguous that could be resolved by overload resolution?

The following program is rejected by gcc as ambiguous: struct Aint { virtual void foo(int); }; struct Astring { virtual void foo(std::string); }; struct A: public Aint, public Astring {}; int main() { std::string s; A a; …
Jens
  • 9,058
  • 2
  • 26
  • 43
9
votes
1 answer

Different behaviour between Clang and GCC when performing qualified name lookup

Consider the following program: #include namespace N { int j = 1; } namespace M { typedef int N; void f() { std::cout << N::j << std::endl; } } int main() { M::f(); } Compiling it with clang gives the following…
Supremum
  • 542
  • 7
  • 23
9
votes
2 answers

static function lookup from a template function issue with xlC

While I was searching for clues about a compilation problem I have had in my source, I have come across this bug report (against Mozilla's JavaScript engine source) related to functions lookup. Quoting from the bug report: TypedArrayTemplate is…
wilx
  • 17,697
  • 6
  • 59
  • 114
9
votes
1 answer

Operator overloading clang++ and g++ different output

With this sample program I observe a different behavior in g++ and clang Foo.h: #include namespace Bar { class Foo { public: Foo(int x) : _x(x) {} int x() const { return _x; } private: int…
José
  • 3,041
  • 8
  • 37
  • 58
8
votes
1 answer

Does the use a simple-template-id in a nested-name-specifier unambiguously mean a class template specialization?

struct A{ template void T(){} }; struct B{ template struct T{ using type = U; }; }; struct C:A,B{ }; int main(){ C::T::type d; } This example is accepted by neither GCC nor Clang. As per…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
8
votes
1 answer

Why overloaded operator== for std::weak_ptr instantiated with type defined in namespace can't be found?

I'm using Visual Studio 2015. Any idea why this code compiles: #include class Foo; class Bar; typedef std::pair> Object; typedef std::vector ObjectVect; bool operator==( std::weak_ptr left, …
jpo38
  • 20,821
  • 10
  • 70
  • 151
8
votes
1 answer

Name lookup issue, GCC and clang disagree

As pointed out by ecatmur, this question already has an answer here. This question is obviously not a duplicate of trailing return type using decltype with a variadic template function. It actually tries to propose a simpler solution to address the…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
8
votes
1 answer

Ambiguous name lookup with using-directive

It's not allowed to put a namespace and a class with the same name into one declarative region, i.e. namespace A {} class A{}; is ill-formed (see §3.3.1/4). However, one can introduce the name of either one via a using-directive: namespace N {…
Columbo
  • 60,038
  • 8
  • 155
  • 203
8
votes
3 answers

C++ enforce second-pass name lookup in template function

Is there some way to force C++ compilers to perform name lookup for a given symbol during template instantiation (and not before)? Given the following code: template auto wrapper( T t ) -> decltype( f( t ) ) { return f( t…
Markus Mayr
  • 4,038
  • 1
  • 20
  • 42
8
votes
2 answers

C++ detecting free function existence with explicit parameters

I'm writing some type traits to see if a free function exists with a specific set of parameters. The functions have a signature that looks something like this: template void func( SomeClass &, SomeType const & ); I know ahead of time the…
Azoth
  • 1,652
  • 16
  • 24
8
votes
4 answers

find() using overloaded operator==

I try to find an element in a vector using overloaded operator==(). However, if using type1 in the following code, the output is 1 and 0 (not found). Using type2 gives both 1 and 1. The environment is Xubuntu 12.04 and g++ version 4.6.3. #include…