Clause 46: When a type conversion is required, define a non-member function for the template

Source: Internet
Author: User

Take a look at the following example:

1Template<typename t>2 classrational{3  Public:4Rational (ConstT & Numerator,ConstT &denominator);5     ConstT numerator ()Const;6     ConstT Denominator ()Const;7 };8Template<typename t>9 ConstRational<t>operator*(Constrational<t>&LHS,Ten                             Constrational<t>&RHS); Onerational<int> Onehalf (1,2); Arational<int> result = onehalf*2;
The last expression above looks as if it could be compiled, but actually not, because the template relationship, the compiler will not know which function we want to call. The above can be pushed out, the correct possibility is that the compiler uses Rational<int> 's non-explicit constructor to convert 2, but the compiler does not put the implicit conversion into the scope of consideration when the argument is pushed down. The effective way to solve the above problem is not to let the operator* need to go through the argument, but to set it as a friend function of rational:
1Template<typename t>2 classrational{3  Public:4Rational (ConstT & Numerator,ConstT &denominator);5     ConstT numerator ()Const;6     ConstT Denominator ()Const;7 friend8     ConstRationaloperator*(ConstRational<t> &LHS,9                             ConstRational<t> &RHS);Ten};
Thus, when the Onehalf declaration comes out, it is equivalent to automatically declare the above operator*, such an implicit conversion can also be successful. In a class template, the template name can be used as the "template parameter of the proposed expression form", so that the above form and the following
1   friend 2     Const operator* (const rational<t> & lhs,3                             const Rational <T> & RHS);
const Rational<T> & rhs);are actually the same. But in fact, the above rational outside to give operator* a definition is not feasible, because the template reason, this definition must be placed inside the class, as follows:
1Template<typename t>2 classrational{3  Public:4Rational (ConstT & Numerator,ConstT &denominator);5     ConstT numerator ()Const;6     ConstT Denominator ()Const;7 friend8     ConstRational<t>operator*(ConstRational<t> &LHS,9                             ConstRational<t> &RHS)Ten     { One         returnRational (Lhs.numerator () *rhs.numerator (), ALhs.denominator () *rhs.denominator ()); -     } -};
The use of friend here actually has its original meaning: in order for the type conversion to occur on all the arguments, a non-member is needed, in order for the function to be automatically materialized, we need to declare them inside the class, The only way to declare the Non-member function inside class is to declare him friend.!! Summary: When writing a class template, the provided "related to this template" function supports "implicit types of parameters", you should define those functions as "friend functions inside the class template."

Clause 46: When a type conversion is required, define a non-member function for the template

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.