What is name lookup mechanism?

后端 未结 3 599
一向
一向 2021-02-09 00:26

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

相关标签:
3条回答
  • 2021-02-09 00:56

    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.

    0 讨论(0)
  • 2021-02-09 00:58

    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

    0 讨论(0)
  • 2021-02-09 01:02

    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.

    0 讨论(0)
提交回复
热议问题