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

What are the rules for namespace search for qualified names?

Can someone explain the behavior I am seeing for namespace search for qualified names in the following example code? See inline comments for the issue. namespace ns1::hw { struct A1 { enum class E1 { E1_1, E1_2, }; …
Ziffusion
  • 8,779
  • 4
  • 29
  • 57
1
vote
3 answers

How can a function acces a matrix?

I have a function,and a matrix declared above it. int M[100][100]; int function(int row , int col) { if (M[row][col] == 1)return 1; return 0; } My question is,how can the function acces the matrix if i don't pass it as a parameter,like: int…
user13528118
1
vote
1 answer

A point from n3290 :Argument-dependent name lookup

A point from n3290 Draft ISO Standard:Section : 3.4.2 ,Point 2nd For each argument type T in the function call, there is a set of zero or more associated namespaces and aset of zero or more associated classes to be considered. The sets of…
user751747
  • 1,129
  • 1
  • 8
  • 17
1
vote
1 answer

Using directive & scope

I have a problem with the folowing code : struct A { A(int i) {}; }; namespace Foo { using Alias = A; } struct B : Foo::Alias { B(); }; B::B() : Alias(5) {} int main() { return 0; } It doesn't compile because in the B constructor, I…
Oodini
  • 829
  • 1
  • 6
  • 16
1
vote
2 answers

Trying to sort map by values but get errors

I have this program with a map and i'm trying to sort them by values but i got errors. Can anyone tell me what I do wrong. Errors are at 28, 29, 30 line. Thanks #include #include #include #include #include…
Kent
  • 13
  • 3
1
vote
3 answers

Function falls completely off the candidate list if it takes a pointer to the class

Why can't I have a non-member function with the same name as a member function, if it also happens to take a pointer to the class? This doesn't compile: struct FooConfig{ int value; }; struct BarConfig{ int value; }; class Api { void…
user2394284
  • 5,520
  • 4
  • 32
  • 38
1
vote
2 answers

Function overload outside class not seen

Consider following code snippet: enum class Bar { A }; void foo(Bar) {} struct Baz { void foo() { foo(Bar::A); } }; It fails to compile, the message from gcc 9.2 is: :12:19: error: no matching function for call to…
bartop
  • 9,971
  • 1
  • 23
  • 54
1
vote
1 answer

How to add a class' function to the overload resoution list within the other function of the same class?

The question is within the code snippet: #include #include #include struct A { static int max(std::pair const& pair) { return std::max(pair.first, pair.second); } int…
1
vote
3 answers

Difference between passing an arguement

Hi All I have written two codes 1. #include using namespace std; void swap(int *x, int *y) { int t; t = *x; *x = *y; *y = t; } int main() { int a = 10, b = 20; …
1
vote
2 answers

Function-definition in class requires same function in other class : compilation error

I have an enum class "Suit" and defined a function "string to_string(Suit e)" In another class, "Card", I have a member variable "my_Suit" and a member-function "to_string". This function calls the function "to_string" with "my_Suit" as a…
1
vote
2 answers

Scope of using namespace versus using namespace closure

I'm trying to understand why there is ambiguity in my functions when I use using namespace versus explicitly declaring a namespace enclosure. Book.h header file: #ifndef MYBOOK_BOOK_H #define MYBOOK_BOOK_H namespace mybook { void showTitle(); …
S. Trahl
  • 99
  • 8
1
vote
2 answers

Question about C++ call virtual function implemented in base from derived class

What is wrong with the following code? struct A { virtual int hash() const = 0; virtual int hash(int x) const = 0; }; struct B : public A { int hash() const final { return 10; }; int hash(int x) const override { return 10; …
1
vote
1 answer

Are unqualified names of non-static data members with dependent types dependent

Dependent names are not clearly defined in the C++ standard, so it leaves a lot to be desired in terms of determining what a dependent name is, which leads me to this question: Are unqualified names of non-static data members with dependent types…
Krystian S
  • 1,586
  • 7
  • 23
1
vote
0 answers

Name lookup in un-opened namespace

Name lookup of specialized template function looks in a namespace that is not opened, and the name lookup is dependent on the types of the parameters in the template parameter pack. It also only happens when I specialize the function, not when I…
Generic Name
  • 1,083
  • 1
  • 12
  • 19
1
vote
1 answer

clang error messages for qualified names and using declaractions

Consider the following code. namespace A::B::C::D::E { struct X { }; } namespace B { using namespace A::B::C::D::E; // or, using A::B::C::D::E::X; // or, using X = A::B::C::D::E::X; } Let's say I use B::X incorrectly in some way…
Anthony
  • 1,015
  • 8
  • 22