C + + compiler's function compilation process

Source: Internet
Author: User



The type lookup process in C + + is relatively simple, basically a name lookup, which is no longer described here.

For a function (or member function) called in a. cpp file, the compiler mainly does the following three things:

1 Name search. First name lookup is performed in the visible name entity in the compilation unit where it is located. (1) The class member function takes precedence (the class that the object resides in, and the base class). Stop the lookup once it is found. (2) If not, do a further search in the appropriate namespace; (3) If not, it will be looked up according to the namespace in which the function parameter resides (keoning lookup).

2 overload resolution. The overload resolution is based on the name found, and the corresponding function is selected according to the principle of the parameter matching.

3 Accessibility checks. Used to determine whether the selected function can be called.

Description

1) According to the first article, it is obvious that if a type wants to work with a non-member function, they should be placed in the same namespace. For example, a generic type of overloaded operators and parameter types are placed in the same header file/or under the same namespace.

2) The function special template does not participate in overload resolution, so if you want to use a function of the special, the best way is to overload the function, in the implementation of the use of this special to work.

3) Overload resolution occurs before the accessibility check. Therefore, if the private function unfortunately participates in the overload and is selected, there will eventually be an unreachable compilation hint. This often implies ambiguity, and the design itself is unreasonable. In other words, private parameters are not "private" when the name is searched and overloaded.

Take the function Call of C.twice (21) as an example:

A) name lookup: The compiler first looks for an entity scope (class, file, or namespace) that contains at least one name twice, and puts the candidate functions into the list of candidate entities. In the example, the compiler first finds the object C in the same class entity, finds it, and, if not found, finds it in its base class and outer namespace, until it finds a scope with at least one candidate function. Two points to note: 1) as long as an entity is found to stop the lookup, not all functions of the same name will be considered; 2) the namespace of the parameter is also in the lookup scope (keoning guideline).

b) Overload resolution: Select the only best match from the list of candidate overloaded functions found. If it is not unique, there is ambiguity. Note: 1) This is based on the name of the search result, 2) the Special template function does not participate in overloading.

c) Accessibility checks: Determines whether the selected function is accessible. This is the last step, later than the overload resolution.

The rules in the article are very important, and after understanding, many C + + compiler problems will be solved naturally. There will be further articles on how to determine the visible names in each C + + compilation unit (CPP file), which are related to the name lookup, the keoning lookup, the overloading (overload), and the implementation of the virtual function (override).

C + + compiler's function compilation process

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.