More effective C + + clause 21 Avoid implicit type conversions with heavy-duty technology

Source: Internet
Author: User

1. As stated in clause 19 and clause 20, the construction and destruction of temporary objects increases the operating costs of the program, so it is necessary to take measures to avoid the creation of temporary objects. Article 20 describes a method for eliminating the return object of a function and producing a temporary object--rvo, However, it does not address the cost of temporary objects resulting from implicit type conversions. In some cases, you might consider using heavy-load techniques to avoid implicit type conversions.

2. Consider the following class Upint classes for handling high-precision integers:

class upint{public:    upint ();    Upint (int  value);    ...}; Const operator+ (const upint& lhs,const upint& RHS);
View Code

Then the following statements can be compiled by:

Upint upi1; Upint  upi2=2+upi1;upi3=upi1+2;
View Code

The reason for this is that the single int parameter constructor of Upint provides a way for an int type to be implicitly converted to the Upint type by calling Upint's single int parameter constructor to create a temporary Upint object, and then calling operator+. This process produces a temporary object, Used to call operator+ and add two Upint objects, but actually add int to upint without an implicit type conversion, in other words, implicit type conversion is only a means, not an end. To avoid the temporary object cost of implicit type conversions, you can use the operator+ To Overload:

operator+ (intoperator+ (const upint&,int);

3.2 The strategy of replacing implicit type conversions with function overloads is not limited to operator functions, but can also be used for compatibility with strings and Char*,complex (complex numbers) and int,double, but this policy is weighed against Because adding a lot of overloaded functions is not necessarily a good thing, unless it does make the program more efficient.

More effective C + + clause 21 to avoid implicit type conversions with heavy-duty technology

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.