Name lookup of C ++: qualified name lookup

Source: Internet
Author: User

Name lookup of C ++: qualified name lookup
1. Introduction

In C ++, Name lookup is a process of finding the corresponding statement through name. For a function, name lookup may match multiple declarations. If it is a function, you can use Argument-dependent lookup to find the statement. If it is a function Template, you can use the Template argument deduction to find the statement, the last step is solved through overload resolution.

There are two types of name lookup: qualified name lookup and unqualified name lookup. Are there the following symbols on the left of the first name.

2. qualified name lookup

A Qualified name appears on the Right of:, which can include class members (methods, types, templates, etc.), namespace members (another namespace), and enumeration.

The rules are as follows:

: If there are no other elements on the left, the top-level namespace is used by default, as shown in the following example:

 

#include 
 
  int main(){  struct std{};  std::cout << fail; // Error: unqualified lookup for 'std' finds the struct  ::std::cout << ok; // OK: ::std finds the namespace std}
 
The order of namelookup is from left to right: The elements on the left will only be used as the command space, class, enumeration, and template. The example is as follows:

 

 

struct A {  static int n;};int main() {  int A;  A::n = 42;    // OK: unqualified lookup of A to the left of :: ignores the variable  A b;          // error: unqualified lookup of A finds the variable A}
If a qualified name is used as one declaration, the lookup of all names with the same declaration after it will be the same as it, except before it.

 

 

class X { };constexpr int number = 100;class C {  class X { };  static const int number = 50;  static X arr[number];};X C::arr[number], brr[number];   // Error  // Every name in the declarator C::arr[number] after C::arr  // is looked up within C::, but the names before C::arr are unaffected,  // The names in the second declarator (brr[number]) are also unaffected  // equivalent to:  // ::X C::arr[C::number], brr[::number]C::X C::arr[number], brr[number]; // Compiles, size of arr is 50, size of brr is 100
If the right side of: Is a destructor, The name lookup of this destructor will be consistent with the name lookup on the left.

 

 

struct C { typedef int I; };typedef int I1, I2;extern int *p, *q;struct A { ~A(){}; };typedef A AB;int main() {    p->C::I::~I(); // the name I after ~ is looked up in the same scope as I before ::    // (that is, within the scope of C, so it finds C::I)    q->I1::~I2();  // The name I2 is looked up in the same scope as I1    // that is, from the current scope, so it finds ::I2    AB x;    x.AB::~AB(); // The name AB after ~ is looked up in the same scope as AB before ::    // that is, from the current scope, so it finds ::AB}
If: is the same class name on both sides, it can only be treated as a constructor. However, when Elaborated type specifier is used, the subsequent functions are ignored.

 

 

struct A { A(); };struct B : A { B(); };A::A() { } // A::A names a constructor, used in a declarationB::B() { } // B::B names a constructor, used in a declarationB::A ba;   // B::A names the type A (looked up in the scope of B)A::A a;    // Error, A::A does not name a type struct A::A a2; // OK: lookup in elaborated type specifier ignores functions                // so A::A simply names the class A as seen from within the scope of A                // (that is, the injected-class-name)

 

Qualified name lookup can be used to access nested or derived hidden class members.

 

struct B { virtual void foo(); };struct D : B { void foo() override; };int main(){    D x;    B& b = x;    b.foo(); // calls D::foo (virtual dispatch)    b.B::foo(); // calls B::foo (static dispatch)}
If the namespace is specified on the left of: Or: no name on the left (using the top-level namespace), The namespace is used on the Right of:, except for the following situations, that is, name is used as the template parameter.

 

 

namespace N {    template
 
   struct foo {};    struct X {};}N::foo
  
    x; // error: X is looked up as ::X, not as N::X
  
 
In the nested reference in the name center, Qualified name lookup is recursively executed. First, it is lookup In the first layer. If it is not, it is lookup in the referenced namespace and continues recursion:

 

 

int x;namespace Y {    void f(float);    void h(int);}namespace Z {    void h(double);}namespace A {    using namespace Y;    void f(int);    void g(int);    int i;}namespace B {    using namespace Z;    void f(char);    int i;}namespace AB {    using namespace A;    using namespace B;    void g();}void h(){    AB::g();  // AB is searched, AB::g found by lookup and is chosen AB::g(void)    // (A and B are not searched)    AB::f(1); // First, AB is searched, there is no f    // Then, A, B are searched    // A::f, B::f found by lookup (but Y is not searched so Y::f is not considered)    // overload resolution picks A::f(int)    AB::x++;    // First, AB is searched, there is no x    // Then A, B are searched. There is no x    // Then Y and Z are searched. There is still no x: this is an error    AB::i++;  // AB is searched, there is no i    // Then A, B are searched. A::i and B::i found by lookup: this is an error    AB::h(16.8);  // First, AB is searched: there is no h    // Then A, B are searched. There is no h    // Then Y and Z are searched.    // lookup finds Y::h and Z::h. Overload resolution picks Z::h(double)}
And the same statement is allowed.

 

 

namespace A { int a; }namespace B { using namespace A; }namespace D { using A::a; }namespace BD {  using namespace B;  using namespace D;}void g(){  BD::a++; // OK: finds the same A::a through B and through D}


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.