C + + Proverbs: Define non-member functions when type conversion

Source: Internet
Author: User
Tags mixed

C + + Proverbs: The timing of declaring non-member functions explains why only non-member functions (non-member functions) are suitable for implicit type conversions (implicit type conversions) that apply to all arguments (arguments). It also uses the operator* function of Rational class as an example. I recommend that you familiarize yourself with that example before you read this article, because this article has done a harmless (templating Rational and operator*) extension discussion of the example in the C + + Proverbs: The timing of declaring a non-member function:

template<typename T>
class Rational {
Rational(const T& numerator = 0, // see《C++箴言:用传引用给const取代传值》for why params
const T& denominator = 1); // are now passed by reference
const T numerator() const; // see《C++箴言:避免返回对象内部构件的句柄》for why return
const T denominator() const; // values are still passed by value,
... // Item 3 for why they're const
template<typename T>
const Rational<T> operator*(const Rational<T>& lhs,
const Rational<T>& rhs)
{ ... }

Like in the C + + Proverbs: The time to declare a non-member function, I want to support Mixed-mode arithmetic (mixed-mode operations), so we need to get the following code to compile. We expect it to, because we use the same code as the code that works in Item 24. The only difference is that Rational and operator* are now templates (template):

Rational<int> oneHalf(1, 2); // this example is from 《C++箴言:声明为非成员函数的时机》,
// except Rational is now a template
Rational<int> result = oneHalf * 2; // error! won't compile

The fact that the compilation failed implies that there is something different from the non-template (non template) version for templated Rational and that it does exist. In the C + + Proverbs: The time to declare a non-member function, the compiler knows what function we want to call (get two rationals operator*), but here the compiler doesn't know which function we want to call. Instead, they attempt to determine what functions to instantiate (that is, create) from the template (template) named operator*. They know that they assume that an instantiated function named operator* obtains two parameters of the rational<t> type, but in order to do this instantiation, they must determine what T is. The problem is that they can't do it.

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.