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
5
votes
2 answers

C++ can't find function out of namespace

Compiling the following code fails because the second function can't find the first one, even though it's outside namespaces. I couldn't figure out the problem myself, and so far I haven't found any answers on the net. test.cpp: #include…
5
votes
1 answer

Unqualified lookup in C++

#include #include #include namespace /*namespace name generated by compiler*/ { struct BB{}; } struct AA{}; namespace my { inline void * memcpy(void*, const void*, std::size_t) { puts("CUSTOM…
Konstantin Burlachenko
  • 5,233
  • 2
  • 41
  • 40
5
votes
3 answers

Why does this code print a randomly selected attribute?

Today while writing some especially terrible code, I stumbled across this mysterious behavior. The Python 3 program below prints a randomly selected attribute of object. How does this happen? An obvious suspect for the nondeterminism is the random…
feersum
  • 658
  • 4
  • 11
5
votes
0 answers

Implementation divergence for program with multiple inheritance and using-declaration with different access specifier than original declaration

Is the following program well-formed or ill-formed according to the c++ standard? struct A { protected: static const int x = 0; }; struct B : A {}; struct C : A { using A::x; }; struct D : B, C {}; int main() { D::x; } Different compilers gives…
5
votes
2 answers

std:: qualifier needed when overloaded namespace function exists?

If I have a little bit of code like: using namespace std; namespace myNamespace { vector sqrt( vector v ) { return v; } void func() { vector myVec = { 1, 2, 3, 4 }; std::cout << sqrt( myVec )[0] <<…
user2746401
  • 3,157
  • 2
  • 21
  • 46
5
votes
1 answer

Strange behavior when perform argument dependent name lookup in template

Recently I was studying the exact meaning of the well-known "two-phase name lookup" for the names in template classes. Although I have read a lot of articles about this, I still cannot know everything about this. Now I was confusing about the code…
sunlight07
  • 686
  • 1
  • 7
  • 21
5
votes
1 answer

Name lookup for overloaded functions defined later

I noticed strange behavior regarding function lookup when relying on a function to be defined later: #include template void foo(const T&) { std::cout << "Basic" << std::endl; } template void bar() { T…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
5
votes
2 answers

Why does C++11 not support name lookup like this?

struct A { enum InnerEnum { X }; A(InnerEnum x) {} }; int main() { A a(X); } The compiler complains: error C2065: 'X' : undeclared identifier The compiler knows what the constructor's parameter type is, so when I pass X as the…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
4
votes
1 answer

How to use BOOST_STATIC_ASSERT

#include #include using namespace std; // I understand how the following template function works // template // T GetMax (T a, T b) { // T result; // result = (a>b)? a : b; // return…
q0987
  • 34,938
  • 69
  • 242
  • 387
4
votes
0 answers

Point of instantiation in complete-class context

template struct A { using U = typename T::U; using V = typename T::V; //X }; struct B { using U = int; void f() { A a; } //1 //A a; //2 using V = int; }; This compiles on current GCC, Clang,…
user17732522
  • 53,019
  • 2
  • 56
  • 105
4
votes
2 answers

Why does the C struct hack not work for C++ template declarations?

C has this fun hack where one name can be used to declare both a type and a function: struct foo {}; void foo(void); Now, any time I write foo, the compiler assumes I mean the function unless I prefix it with struct: foo(); // call void…
Filipp
  • 1,843
  • 12
  • 26
4
votes
2 answers

What happens when Injected-Class-Name occurs? (C++)

According to https://en.cppreference.com/w/cpp/language/injected-class-name In a class scope, the name of the current class is treated as if it were a public member name; this is called injected-class-name. The point of declaration of the name…
csguy
  • 1,354
  • 2
  • 17
  • 37
4
votes
4 answers

Namespace information compromises the readability in C++

I'm new to C++ and had a background in C. The one thing which is quite difficult for me to adopt is to frequently use scope operator e.g. std:: Well, i'd avoid it usage by putting using namespace std at the start of my source code but a lot of…
Nouman Tajik
  • 433
  • 1
  • 5
  • 18
4
votes
2 answers

Name lookup in template base: why do we add this->

Consider the following: struct Base { void foo(); }; template struct Derived : Base { void bar() { this->foo(); } }; Typically, we explain that this-> makes foo() a dependent name, so its lookup is posponed to the…
4
votes
3 answers

What is the type of a constructor?

I know constructor do not have a return type. Though I wonder, what is the type of a constructor then? Does a constructor have a type? I tried this struct A { A() {} }; template struct foo; int main() { foo< decltype(&A::A) > f; } to…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185