Next is the more effective C + + 11 to 20 section:
11. Suppress exception information (exceptions) from being passed to the destructor.
There are two possible types of destructors: (1) Normal destruction of objects (2) stack unwinding mechanism during abnormal propagation-destroy.
If an exception is thrown within a destructor, it is not captured by the destructor, it propagates to the call side of the destructor, if the caller is
Called, then the program will die. It is also possible that the subsequent statement cannot be executed, so the exception cannot be propagated outside the destructor.
12. Understand the difference between "throw an exception" and "pass a parameter" or "invoke a virtual function."
(1) Thrown exception is always copied (value pass or reference pass), the pointer will not be copied, trows; throws the current exception, throws type; The static type that throws the exception.
(2) The thrown exception does not convert the type, but it can catch a subtype exception that inherits the parent class.
(3) The order of handling exceptions is first caught first, so the class exception is placed before the parent class exception.
13. Catch an exception by reference (reference).
By referencing a catch exception, you can resolve the exception if the pointer is a local object (throwing garbage that is destroyed).
Or throw a pointer to the object in the heap of new that should not be removed after the annoyance.
It also solves the problem of the efficiency of two copies caused by value passing and the polymorphic problems of virtual function calls (replication of exceptions is a copy of the static type).
14. Use the exception specification (exception specifications) with caution.
In the function that gives the exception specification, if the function inadvertently violates the exception specification in the exception list, it will cause the program to terminate.
It can also cause a higher-level caller to be ready to handle the exception and cannot be processed. You can also solve this problem by replacing unexpected exceptions with different types of exceptions.
15. Understand the overhead of exception handling.
The usage cost of the exception is quite high, limit the use of the TRY statement block and exception specification to a place where it is not available, and throw an exception in the case of a real exception.
16. Remember the 80-20 rule
A program 80% of the resources used for 20% of the code body. 80% of the memory is used by approximately 20% of the code, and 80% of the execution time is spent on about 20% of the code.
When you need to find the bottleneck of the problem, the best is to find the key 20% of the code, you can use a program parser to analyze, know that the statement is executed or function
The frequency of calls can also give you some insight.
17. Consider using lazy evaluation.
The rule of thumb is simply to calculate the amount of unnecessary and complex computations that are not necessary until they are actually used, to improve efficiency.
However, really need to avoid the large number of operations, that is to pay the corresponding price.
18. Amortization is also expected to calculate the cost.
This clause corresponds to the previous one, which is equivalent to pre-allocating the required memory, or pre-recording frequently used data, using space to exchange time.
19. Understand the source of the temporary object.
The temporary object is not a local object, it will not appear in your source code.
There are two possible types of temporary object generation: (1) When implicit type conversion. (2) When the function returns the object.
An implicit type conversion occurs only when a parameter is passed as a value pass or a constant reference, and an implicit conversion does not occur when a very-referenced object parameter is passed.
Be careful to avoid implicit type conversions and optimize the return value.
20. Assist to complete the "Return value optimization Rvo".
An inline function can be used to optimize the return value, which eliminates the local and temporary objects that are returned.
Not to be continued ~
More effective C + + (2)