Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is name lookup mechanism?

Tags:

c++

I'd like to know what C++ name lookup mechanism is.

like image 314
Alex Avatar asked Apr 08 '10 22:04

Alex


2 Answers

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.

  1. scope -> directory
  2. object -> file

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.

like image 125
Life Avatar answered Oct 31 '22 04:10

Life


Name lookup is the process of identifying what a name means. Name lookup has two purposes

  • Disambiguate parsing of your code
  • Determining what precisely your code means

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

  • Unqualified name lookup: Starting from the current scope, a name is looked up, escaping into the enclosing scopes and base classes if inside a class. Does not start from a specific named scope. This lookup form stops as soon as it finds a name. So a name in an inner scope (or class) hides a name found in an outer scope (or base class).
  • Qualified name lookup: Looking a name up in a given scope using the :: 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.

like image 33
Johannes Schaub - litb Avatar answered Oct 31 '22 04:10

Johannes Schaub - litb