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
6
votes
0 answers

Template instantiation, two-phase name lookup, different behavior with automatic deduced type

After seeing this question When is a C++ template instantiation type checked? , and wondering myself for quite some time the same thing I started to play with code to assimilate the knowledge. The answer gives clear and correct explanation. It…
bolov
  • 72,283
  • 15
  • 145
  • 224
6
votes
1 answer

what is the equivalent of :: operator in D?

I've just started learning D. In C++ there is :: (Scope resolution operator) to access global variable from the function if both global & local varible have same name. But how to do this in D language? Consider this program. import std.stdio; int…
Destructor
  • 14,123
  • 11
  • 61
  • 126
6
votes
2 answers

Name lookup in using-declaration via using-directive

Is the following program well-formed or ill-formed according to the c++ standard? namespace N { int i; } using namespace N; using ::i; int main() {} I get different results with different compilers: Clang…
6
votes
1 answer

Name lookup for local class members inside templates

Consider the following code, that simulates a constexpr lambda (proposed for C++17, not available in C++14). #include template constexpr auto fun(Pred pred) { return pred(1) <= M; } template struct C { …
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
6
votes
1 answer

Is this a bug in GCC?

EDIT: This is not a bug, just me not knowing about dependent name lookups in templated base classes (which MSVC "helpfully" resolves without errors). I wrote a functor implementation a while back, and a simple "Event" wrapper that uses it. It…
Cameron
  • 96,106
  • 25
  • 196
  • 225
5
votes
1 answer

What is the difference between calling foo() and ::foo() within a C++ class member function?

I am looking at someone else's C++ code (note I am not fluent in C++). Within the class, there is this member function: void ClassYaba::funcname() { ... ::foo(); ... } There is no member function within that class's namespace named foo,…
Ed.
  • 928
  • 1
  • 10
  • 23
5
votes
3 answers

ISO C++ draft - 3.4.2/3 - Argument Dependant Name Lookup

A point from the ISO C++ draft (n3290): 3.4.2/3 Argument Dependant Name Lookup: Let X be the lookup set produced by unqualified lookup (3.4.1) and let Y be the lookup set produced by argument dependent lookup (defined as follows). If X…
user751747
  • 1,129
  • 1
  • 8
  • 17
5
votes
2 answers

Ambiguous name lookup with C++20 using-enum-declaration

Consider following code snippet with C++20 using-enum-declaration: namespace A { enum A {}; }; using namespace A; using enum A; gcc-trunk rejects it with: :4:12: error: reference to 'A' is ambiguous 4 | using enum A; | …
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
5
votes
1 answer

namelookup with Unqualified name : C++0x draft n3290

A point from the ISO C++ Draft n3290 : 3.4.0 2nd point A name “looked up in the context of an expression” is looked up as an unqualified name in the scope where the expression is found. Would someone please explain this statement with an example?
user751747
  • 1,129
  • 1
  • 8
  • 17
5
votes
1 answer

Different compiler behaviour when using alias as scope to get parent member

This code compiles fine on Clang and Visual C++ but not on GCC: #include template struct Test { Test(T &t) : _t(t) { } void method() { std::cout << _t.Internal::_value << "\n"; // Doesn't work on…
Johnmph
  • 3,391
  • 24
  • 32
5
votes
1 answer

Point of instantiation of default arguments in a template function

The standard allows function templates to be instantiated after the enclosing namespace-scope declaration or at the end of the translation unit when they are referred to from a non-template context: [temp.point]/1 For a function template…
L. F.
  • 19,445
  • 8
  • 48
  • 82
5
votes
1 answer

What is the definition of "dependent name" in C++?

In C++, the concept of dependent names is important because: Such names are unbound and are looked up at the point of the template instantiation ... in both the context of the template definition and the context of the point of…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
5
votes
0 answers

How to make all hidden names from a base class accessible in derived one?

Starting from this question: Pointer derived from pure virtual class(A) can't access overload method from the pure class (B) And considering this simplified code: #include #include class Abstract { public: virtual void…
j4x
  • 3,595
  • 3
  • 33
  • 64
5
votes
1 answer

Two-phase function template compilation: not *only* ADL is employed in the 2nd phase?

I'm wondering why the following code compiles. #include template void print(T t) { std::cout << t; } namespace ns { struct A {}; } std::ostream& operator<<(std::ostream& out, ns::A) { return out << "hi!"; } int…
5
votes
1 answer

Can defining size_t in my own namespace create ambiguity or other bugs?

I have the following code which defines size_t equivalent to std::size_t and ::size_t if I included . // h.hpp namespace N { using size_t = decltype(sizeof(int)); } // a.hpp #include namespace N { class C { size_t size()…
palotasb
  • 4,108
  • 3
  • 24
  • 32