In-depth exploration of C ++ object model Reading Notes-Chapter 7 on the cutting-edge of Object Model

Source: Internet
Author: User
Template

The template is inCompilation PeriodRather than being calculated during execution. Therefore, it will not reduce the efficiency.

   1: const Point<float> &ref = 0;

This statement instantiates a point float instance. This statement is extended:

   1: Point<float> temp(float(0));
   2: const Point<float> &ref = temp;

This is because 0 must be converted to an object before it can be referenced. If it cannot be converted, this definition is incorrect and will be found during compilation.

That is, the definition of a class object will not be performed by the compiler in the dark (temporary object) or by the programmer (create object), which will lead to the instantiation of the template class.

The other side of C ++ requires that for memberfunctions (member functions), unused ones should not be instantiated and will only be instantiated when memberfunctions is used.

(However, the current compiler does not precisely follow this requirement .)This requirement is mainly based on the following two reasons:

  1. Space and time efficiency considerations. For example, there are 100 member functions in the class, and your program value uses two of them for a type and five for another type, the instantiation of other functions will take a lot of time and space, but it is not actually needed.
  2. Functions that have not yet been implemented. Not all types of template instantiation can fully support the functions required by member functions. If only the functions actually used are instantiated, the template supports the types that instantiate all functions that may cause compilation errors.

In template, the resolution result for a non-member name is based on whether the name is usedParameter type used to instantiate the TemplateRelated.

  1. If it is not related to the instantiation type, the name is determined by "Scope of the template declaration" (that is, the program that defines the template)
  2. If there is an association with the instantiation type, the name is determined by "Scope of the template instantiation" (that is, the program that instantiates the template)
   1: // scope of the template definition
   2: extern double foo(double);
   3:  
   4: templte<class type>
   5: class ScopeRules {
   6:   public:
   7:     void invariant() {
   8:       _member = foo(_val);
   9:     }
  10:     type type_dependent() {
  11:       return foo(_member);
  12:     }
  13:   private:
  14:     int _val;
  15:     type _member;
  16: };
  17:  
  18: // scope of the template instantiation
  19: extern int foo(int)
  20: // ...
  21: ScopeRules<int> sr0;
In scoperules template, there are two Foo () call operations. In scope of template definition, only one Foo () function declaration is in scope. In scope of template instantiation, both Foo () function declarations are located in scope. In this case, if there is a function call operation: sr0.invariant () according to the above rule, the function is irrelevant to the parameter type used to instantiate the template, and it is determined by scope of the template declaration, in this scope, there is only one Foo () candidate, namely Foo (double). In this case, for another function operation: sr0.type _ dependent (), this function is related to the template type to be instantiated, this example uses scope of the template instantiation. In this scope, there are two Foo () candidates. Since _ member is of the int type, the end of Foo (INT) is called, if scoperules is instantiated as a class type and the class does not implement the conversion operator for Int or double, the Foo () call operation is marked as an error. Note: The test code is as follows: Test environment: Test code:Test result: if the code is changed to: The test result is: I don't know if it is a compiler problem, According to the test results, function calls are determined by the template declaration domain.Exception Handling

Three main components of C ++ exception handling:

  1. OneThrowStatement. It sends an exception somewhere in the program. The thrown exception can be of the built-in or custom type.
  2. One or moreCatchStatement. Each catch statement is an exception handler. This clause is used to indicate that the clause is ready to process a certain type of exception and provides the actual processing logic in the closed braces section.
  3. A try partition. It is centered around a series of statements, which may cause catch statements to take effect.

When an exception is thrown, the control is released from the function call and a suitable catch statement is found. If no, the default HandlerTerminate ()Will be called.

When an exception occurs, the compilation system must complete the following tasks:

  1. Checks the function that has throw operations.
  2. Determines whether the throw operation occurs in the try partition.
  3. If so, the compilation system must compare the exception type with each catch clause.
  4. If the comparison is correct, the control process is handed over to the catch clause.
  5. If the throw operation does not occur in the try partition, or no catch clause matches, the system must
    1. Destroy all active local objects (clear resources)
    2. The current function unwind from the battle
    3. In the next function of the stack, repeat the above 2 ~ 5

On the mechanism and principle of exceptions, online to find a good information: Favorite under: http://blog.csdn.net/lovemdx/article/details/9936089

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.