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

Two-phase name lookup: PODs vs. custom types

When compiling and running the code #include struct A { A(int){} }; void foo( int ) { std::cout << "foo(int)" << std::endl; } template< typename T > struct S { void bar() { foo( 5 ); } void foobar() { T t = 5; foo(t);…
proto
  • 158
  • 5
3
votes
3 answers

gcc compiler does not show correct errors with class templates injected names

Recently, I was reading the book: C++ templates: the complete guide written by David Vandevoorde and Nicolai M. Josuttis. Specifically about template parsing quoting from the book pp 126. Class templates also have injected class names, However,…
taocp
  • 23,276
  • 10
  • 49
  • 62
3
votes
1 answer

Compile error when overloading operator<< with template arguments

I'm trying to use the stl copy () to print the key-value pair in a map. The code is as follows: #include #include #include #include using namespace std; //compile error if I comment out "namespace…
3
votes
1 answer

Where does the C++ 98 standard specify that locally declared template names are not dependent?

According to this page: http://womble.decadent.org.uk/c++/template-faq.html#non-dependent "Non-dependent names are those names that are considered not to depend upon the template parameters, plus the name of the template itself and names declared…
willj
  • 2,991
  • 12
  • 24
3
votes
2 answers

How can I get the name of a function from a symbol in clojure?

Suppose I define x as symbol function foo (defn foo [x] x) (def x foo) Can the name "foo" be discovered if only given x? Is there a way within foo to look up the name of the function x - "foo" in this case? (foo x) Is there or is it possible to…
dansalmo
  • 11,506
  • 5
  • 58
  • 53
3
votes
2 answers

Template Member Function Overloading and Multiple Inheritance in C++

I am observing behavior in the below code which I cannot readily explain and would like to understand the theory of better. I cannot seem to find an online documentation source or existing question which covers this particular situation. For…
2
votes
1 answer

Name lookup of friend function in local class

Compiling the following: void bar() { /* ... */ } void foo() { struct MyStruct { friend void bar(); }; } int main() { //.. } results in the error: error: friend declaration 'void bar()' in local class without prior…
user1086635
  • 1,536
  • 2
  • 15
  • 21
2
votes
1 answer

c++ customization point object pattern. Argument and customization in different namespaces

I recently learned about customization point object pattern and tried implementing it. At first, it looked like a very good way to make some base functionality and extend it on different types. In examples below I use Visual Studio 2022 with c++20…
2
votes
1 answer

Why doesn't the << operator defined inside a namespace participate in overload resolution?

In the following scenario, the operator<< function participates in overload resolution (when it is in the global namespace): template std::ostream &operator<<(std::ostream &os, const std::vector &vec) { os << '['; for (const…
2
votes
2 answers

variables declaration in anonymous namespace and definition in other place

Can someone explain why I can't define variable that was declared in anonymous namespace as global variable in another place? #include namespace { extern int number; } int number = 123; void g() { std::cout <<…
notamaster
  • 53
  • 4
2
votes
2 answers

Impact of namespaces on C++ template deduction priority

while trying to implement a metafunction, which needs to exist only if the "abs" function exists for some type, i ran into the folowing problem: Here are two examples of code i would expect to yield the same results but in fact, they do not: First…
J.M
  • 313
  • 1
  • 8
2
votes
1 answer

How the process of member look up occurs in C++?

I'm using the document number 4901, C++ Draft ISO 2021, specifically 6.5.2 (Member Name Lookup). I'm failing in understanding a lot of uses of the terms "member subobject" and "base class subobjects". I already asked about these terms in : What is a…
user15071942
2
votes
1 answer

Lookup of dependent qualified names

This program does not compile (error: 'foo' is not a member of 'N'): namespace N { // void foo(); } template void template_func(T t) { N::foo(t); } But if we uncomment the declaration of void foo();, it compiles. Demo. Both…
Dr. Gut
  • 2,053
  • 7
  • 26
2
votes
1 answer

problem when I separate code to header file

this program works correctly but when I separate that "Class StudentsName" and put it on the header file it doesn't work correctly. I add the header file in my project by right click on the header folder and choosing the new item and the choosing…
asipoolika
  • 41
  • 3
2
votes
2 answers

How to avoid function name conflict in a C library?

I'm programming a serial port on Unix, and I'm using the header file unistd.h. It contains the function: read(int fd, void *buf, size_t count) I'm making a class to call this function, and one of the methods in my class is also called read() to…
John Sall
  • 1,027
  • 1
  • 12
  • 25