"C++11" explicit conversion operator

Source: Internet
Author: User

Implicit type conversion is a good and bad feature of C + +. It is convenient, but it can cause some very vague mistakes.


Type conversions provide a type of construct to another type.

Class X{public:operator int () const noexcept {return 42;    }};void Func (int) {}int wmain () {X x0;    X X1;    Func (x0);    Func (x1);    int n = x0 + x1; Std::cout << n << std::endl; return 0;}

In the above code, X can be implicitly converted to int, so the function func can accept the x type parameter, x0 and X1 can also use + to do the operation.

One of the more common examples of practical programming is that our own defined string class (recorded as String) overloads the operator Const wchar_t* ():

Class String{public:operator Const wchar_t* () const NOEXCEPT {//function body}};

Thus, if a function requires a parameter of the const wchar_t* type, it can pass directly to a string instance.


However, overloaded type conversions are also not foolproof. For example, overload operator BOOL ().

Overloading operator BOOL () brings more problems, so that many enterprise coding specifications do not advocate or even prohibit overloading operator bool ().

Because bool is an arithmetic type, an instance of a class that is overloaded with operator BOOL () can be used in any context that requires an arithmetic type.

Class Y{private:int m_;public:explicit Y (int m): m_{m} {} operator bool () const NOEXCEPT {return (    M_! = 0);    }};int wmain () {Y y0{12};    Y y1{25}; Auto n = y0 + y1;    // !!!    Std::cout << n << std::endl; return 0;}

Meaningless y0 + y1 unexpectedly (without warning) compiled through, but also through the + produced an int, this is really unreasonable. Perhaps the program author wanted y (38), and more likely the person who later defended the code had no way of knowing what the original author was trying to do. As the size of the code becomes larger, these subtle pitfalls will be buried deeper, and it may be the result of a bug that takes two days to find in the future.


To prevent such anomalies, C++11 introduces explicit type conversion operators .

Class x{public:explicit operator int () const noexcept {return 42;    }};void Func (int) {}int wmain () {X x0; Func (x0); Error, there is no (implicit) conversion of int y = x0 from X to int; Error, there is no (implicit) conversion of Func ((int) x0) from X to int; Correct 1 Func (int (x0)); Correct 2 Func (static_cast<int> (x0)); Correct 3 return 0;}

Type conversion operators that are modified with explicit, the corresponding type conversions must be performed explicitly. C-Type (correct 1), functional (correct 2), static_cast(correct 3) are OK.


However, there is an exception to explicit type conversions. If an expression is used as a condition, an explicit type conversion can also be done implicitly. "Being used as a condition" is:

    • The condition part of the IF, while and do statements;

    • The conditional expression for the For statement header;

    • Logical non-operator (!), logical, or operator (| | ), logical AND Operator (&&) operands;

    • The conditional operator (x? Y:Z) of the conditional expression.

Since conversion to bool is generally used as a condition, operator bool () is generally decorated with explicit.

Class k{public:explicit operator bool () const NOEXCEPT {return false;    }};int wmain () {K k0;    if (k0)//correct {std::cout << "qwer" << Std::endl;    } else {std::cout << "ZXCV" << Std::endl; } return 0;}

"C++11" explicit conversion operator

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.