C + + name lookup with ADL

Source: Internet
Author: User

An important work of the compiler is to parse the name it encounters, in general, the compiler is in order to parse the name encountered, for example, the compiler encountered a function, then will generate code for this function, but if the function called another function, and this function has not been parsed, Then the compiler will make an error, such as the following example:

void H () {f ();} void F () {cout<< "F ()" <<endl;}

Because the compiler encounters H () first, but when it starts parsing H (), it finds an F () that hasn't been parsed yet, so it's an error, and if you put the definition of f () before H () there's no problem. This is the general case, the compiler should be smarter, so there is the ADL, that is, the association parameter lookup, that is, the compiler will find the function parameters associated with the scope, compare the following two examples can be found:

Example 1:

Test1.cpp

Class A {int i;public:friend void f ();}; int main (void) {f ();    return 0;}
Tesp2.cpp

Class A;void F () {}
The error "F not found" appears when compiling with the following command: g++-o test1.cpp test2.cpp, take another look at the second example:

Example 2:

Test1.cpp

Class A {int i;public:friend void F (a &a);}; int main (void) {a a;f (a);    return 0;}
Test2.cpp

Class A;void F (A &a) {}

Now, using the same command to compile, there is no error.

C + + name lookup with ADL

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.