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

Name lookup, point of instantiation (POI) and fundamental types

The following code compiles for X, but not for double: struct X{}; void foo(double); void foo(X); namespace NN { struct A{}; void foo(A) { foo(double{}); // error: foo not found foo(X{}); } } The problem here seems to be, that ADL kicks…
krzikalla
  • 73
  • 3
2
votes
2 answers

Difference in behaviour between clang and gcc when trying to confuse them by using a template alias with a dummy parameter to conceal the base class

Consider the following c++ program: class A { protected: int x; }; template using B = A; template class C : public B { public: void f() { x = 0; } }; int…
Supremum
  • 542
  • 7
  • 23
2
votes
2 answers

Overloaded child class function cannot call parent of similar name

I am assuming this is one of those "just not how it works" issues, but I fail to see why. Why do I need to qualify B's call to As Start with A::. If I change B::Start() to B::DoSomethingElse() I could call a parameter less Start() without A::. So…
user13861203
2
votes
1 answer

Unqualified function call selects function from a wrong namespace

Consider this code: #include namespace A { struct Mine {}; template void foo(T1, T2) { std::cout << "A::foo" << std::endl; } } namespace B { template void…
Jakub Klinkovský
  • 1,248
  • 1
  • 12
  • 33
2
votes
1 answer

Simple way to reference member variables of base class templates

Is there any way to reference member variables of base class templates without base class names and scope resolution operator? template struct B0 { int value; }; struct D0: B0 { D0() { B0::value = 1; // OK. …
chaosink
  • 1,329
  • 13
  • 27
2
votes
1 answer

A confusion about function name lookup

I'm confused by some rules in the standard. I'll cite them here: [basic.lookup.argdep]: Let X be the lookup set produced by unqualified lookup and let Y be the lookup set produced by argument dependent lookup (defined as follows). So the sentence…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
2
votes
1 answer

Ambiguous overload for 'operator==' with own class and std::string_view

I created a class named MyClass in namespace N. Now I define a global operator== in namespace N. namespace N { class MyClass { // member function begin() // member function end() // content omitted } template bool operator==(const…
2
votes
1 answer

Overloading Assignment Operator in Base Class

I have a templated class called BaseSignal, template class BaseSignal { public: // Constructor BaseSignal(long buf_size, bool is_async, SimData *sim) .... From this class are derived two other templated classes, Net…
Colin Weltin-Wu
  • 347
  • 1
  • 2
  • 10
2
votes
2 answers

Is it strictly defined by the standard how this program should compile?

The following program was constructed to abuse some peculiarities of two-phase lookup in gcc/msvc. It compiles fine with both gcc/msvc and clang but results in different return value from function g: struct A; struct C {}; struct D { D (const A…
Predelnik
  • 5,066
  • 2
  • 24
  • 36
2
votes
1 answer

Example for dependent name lookup in case when noexcept-specification is needed but not instantiated

From cppreference: When the noexcept-specification of a function template specialization is needed, but hasn't yet been instantiated, the dependent names are looked up and any templates used in the expression are instantiated as if for the…
ledonter
  • 1,269
  • 9
  • 27
2
votes
1 answer

Where is name lookup rule defined that finds the most immediate declaration of a name?

int i; void f() { int i{}; { int a = i; // local or global 'i'? } } My question is not which i gets chosen, as it's clear that it's the local one, but rather, where in the standard that is specified. The closest rule I could…
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
2
votes
1 answer

cannot access namespace scope friend explicitly

I had an issue today where ADL wasn't finding a static member function for a type defined inside a class. That is, in the below example, str(foo::Foo::Enum) isn't located via ADL without explicitly scoping it, foo::Foo::str(foo::Foo::Enum) namespace…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
2
votes
2 answers

C++ function pointers name lookup inside function template

I am trying to understand the name lookup and argument dependency lookup.I have created a small example. Edited: https://godbolt.org/g/rMWUbe #include void g(const int*) {} template struct TypeResolution; template…
2
votes
4 answers

C++ class member name lookup issues (regarding the wording of standard n3225)

I am very confused about the standard 10.2/13, [ Note: Even if the result of name lookup is unambiguous, use of a name found in multiple subobjects might still be ambiguous (4.11, 5.2.5, 5.3.1, 11.2).—end note ] [ Example: struct B1 { void f(); …
user534498
  • 3,926
  • 5
  • 27
  • 52
2
votes
2 answers

Overriding operator<< for all types

I'm a bit annoyed with compilation errors which arise when I try to write std::cout << x, and left shift operator it not defined for x. Can't convert x to this, can't convert x to that... Several screens of useless error messages. I want to…
Ivan Smirnov
  • 4,365
  • 19
  • 30