Hi!
I'd like to know what C++ name lookup mechanism is.
Thank you for your help :)
Hi!
I'd like to know what C++ name lookup mechanism is.
Thank you for your help :)
At its core, it's the process that the compiler uses to figure out what the given name corresponds to - be a it a variable or a function or some other language construct. It has to find the underlying language construct that the name refers to.
e.g. When you call the function printf(), the compiler has to find the declaration for printf so that it understands what it is and can properly compile it.
As previously pointed out, there are various name lookup mechanisms that C++ uses, and you can find information on them pretty easily with google. Wikipedia has some basic information on it as well: http://en.wikipedia.org/wiki/Name_resolution
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.