More effective C + + clause 22 consider replacing its single form (OP) with the operator compound form (op=)

Source: Internet
Author: User

1. In general, the arithmetic operator (hereinafter "celibacy") is overloaded, and the compound assignment operator (hereinafter "compound form") is overloaded. To ensure that the compound form of the operator, such as (operator+=) and celibacy (for example, operator+) behaves in a consistent manner, It is a good way to achieve the latter based on the former. For example:

class rational{public:    operator+ = (const rational&)   ; operator+ (const T&LHS,const t&RHS) {    return T ( LHS) + =RHS;}
View Code

2. The compound form of the operator is usually more efficient than its single form: the single Form needs to return the new object, thus it is necessary to assume the structure and composition of the temporary object, the compound form directly writes the result to the right-end variable, does not need the temporary object's construction process, therefore the following uses the single form the wording:

Rational a,b,c,d; Rational result=a+b+c+d;

Efficiency does not use a compound form of writing High:

Rational a,b,c,d; Rational result=a;result+=b+=c+=d;

Because the former uses operator+ to produce 3 temporary objects, even if there is Rvo (return value optimization, see clause 21), it only occurs in the last operator+, and the first two operator+ constructed temporary objects will not be optimized (explained later, If you follow the implementation of operator+ in 1, even the temporary objects produced by the last operator+ are not optimized, so there are 3 temporary objects instead of 2.

The former is easy to write, maintenance, debugging and the latter is more efficient, the assembler programmer more intuitive.

3. "More effective C + +" explains how operator is implemented in 1

operator+ (const T&LHS,const t&RHS) {    return T (LHS) + =  RHS;}

Reason (for illustrative purposes, the above implementation is version 1), claiming that returning anonymous objects is more likely to cause the compiler to rvo than returning a named object, but the so-called "return anonymous object is more likely to make Rvo than the compiler" is based on two bases: 1.NRVO (named return value optimization, See clause 21) Not yet present; 2. Returns an anonymous object that returns only one anonymous object without further action on it.

For the following actions:

Rational A, B; Rational c=a+b;

operator+ version 1 is actually unable to complete RVO: The final step of the version is return T (LHS) +=RHS, which is not the temporary object T (LHS), but the result of the temporary object calling operator+=, at which point the compiler does not know who is the result to return (T (LHS) This temporary object is obviously not, because there is a follow-up operation), so can only be honest to T (LHS) call operator+=, and then copy the results of the temporary object to C, the production of temporary objects can not be avoided.

In other words, "more effective C + +" advocated by this method is now out of date, in the Nrvo has become popular today, operator+ the following implementations (called Version 2) but more to help the compiler to achieve optimization:

operator+ (const RATIONAL&LHS,const ration&RHS) {    Rational temp (LHS);    Temp+ =RHS    ; return temp;}
View Code

Here are the experimental code and the results in Win7-32,visual Studio release mode:

#include <iostream>using namespacestd;classrational{ Public: Rational&operator+=(Constrational&RHS) {Numerator+=Rhs.numerator; Denominator+=Rhs.denominator; return* This; } Rational (Constrational&RHS) {cout<<"copy Constructor"<<Endl; } Rational (): Numerator (0), Denominator (0) {cout <<"Constructor"<<Endl;}Private:    intnumerator; intdenominator;}; Rationaloperator+(Constrational& LHS,Constrational&RHS) {    returnRational (LHS) + =RHS;}intMain () {Rational A, B; Rational C= A +b; System ("Pause"); return 0;}
View Code

Run the result as

A total of two copy constructor were called, and the optimizations were not implemented.

Operator+ to implement after 2:

operator+ (constconst rational& RHS) {    Rational temp (LHS);     + = RHS    ; return temp;}
View Code

The experimental results are as follows:

Only one copy constructor is called, and it is visible that the temp in operator+ is replaced with the outer C, which is Nrvo optimized.

The detailed explanation of the above experiments can be referred to http://www.xuebuyuan.com/1595871.html, because the experimental platform is different, the experimental results will differ, but the central idea is the same.

More effective C + + clause 22 consider replacing its single form (OP) with the operator compound form (op=)

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.