I'd like to know what C++ name lookup mechanism is.
I don't know whether you ask for an analogy to describe what name lookup is. But I will give a possible answer to the above question. Before looking deeply into name lookup mechanism, let's mapping two concepts for fun and use.
I don't want to explain the reason for this analogy. In the following literal way of thinking, replace scope by directory every time scope is encountered. So does object by file.
#include <iostream>
using namespace std;
namespace Newton{
double distance, time;
double velocity(const double &, const double &);
}
namespace Einstein{
double distance, time;
double velocity(const double &, const double &);
}
int main()
{
using namespace Newton;
double s(10), t(10);
velocity(s,t);
return 0;
}
double Newton::velocity(const double & s, const double & t){
distance = s;
time = t;
cout << "Calculation by Newton" << endl;
return distance / time;
}
double Einstein::velocity(const double & s, const double & t){
distance = s;
time = t;
cout << "Calculation by Einstein" << endl;
return distance / time;
}
in this code, what is the implementation of velocity
? We meet velocity
funciton in the socpe of main()
. If you name it like a path name in the file explorer, velocity
is actually /Main/velocity
. Of course, if you check object names under /Main/
, there are still two double objects, s
and t
, and one namespace object, Newton
. If you list the names of objects under /Main/double/
, I think the built-in function does not matches an object named velocity
, which means, for example, there is no such object -- /Built-in Types/double/velocity
. If you list the names of objects again under /Main/Newton/
, the actual directory is search is /Newton/
because it is declared there. Then, list the object names under /Newton/
, we find two double objects, distance
and time
, and one function named velocity
. Yes, we find a candidate function for /Main/velocity
.
I can only give an analogy of name lookup in c++. There is more to add to conclude a mechanism.
Name lookup is the process of identifying what a name means. Name lookup has two purposes
For instance if you have this code
T(a);
It depends on whether T
is a type or not: If it is a type, it will be a declaration of a
, and if it isn't a type, it's interpreted as a function call.
Some names denote types or templates. In general, whenever a name is encountered it is necessary to determine whether that name denotes one of these entities before continuing to parse the program that contains it. The process that determines this is called name lookup.
Name lookup associates the use of a name with a declaration (3.1) of that name.
There are two main classes of name-lookup
::
operator.Several other forms exist, like looking up a name that appears after the dot or arrow (like ptr->foo
) or looking up a name in class foo
(in which nontype names are ignored). One particular interesting form is the argument dependent lookup used for finding function declarations based on argument types used in a function call.
After name lookup found a declaration, it's inspected to see what attributes it got and whether the program can use it.
Only after name lookup, function overload resolution (if applicable) and access checking have succeeded are the attributes introduced by the name’s declaration used further in expression processing
Thus name-lookup will find private class members, but your code is rejected if you use such names if you haven't access to them. This is true even if a base class would have the same name with public access - that's because name-lookup stops in the derived class if it finds a name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With