C++11 Explicit conversion operators

Source: Internet
Author: User

Explicit conversion operators have been supported prior to C++11

#include <iostream>using namespace std;template <typename t>class ptr {public:    ptr (t* p): _p (P) {}    operator BOOL () const {        if (_p! = 0)            return true;        else            return false;    } Private:    t* _p;}; int main () {    int A;    Ptr<int> p (&a);    if (p)      //Automatic conversion to bool type, no problem        cout << "valid pointer." << Endl   ; Valid pointer.    else        cout << "Invalid pointer." << Endl;    Ptr<double> PD (0);    cout << p + PD << Endl; 1, add, no meaning in semantics}

Explicit conversion operators have been abused.

In c++11, the standard extends the use of explicit to custom type conversion operators to support so-called explicit type conversions. The explicit keyword is used on the type conversion operator, which means that the type can be used only when the target type or explicit type conversion is directly constructed.
Class ConvertTo {};class convertable {public:    explicit operator ConvertTo () const {return ConvertTo ();}}; void Func (ConvertTo ct) {}void test () {    convertable C;    ConvertTo CT (c);        Direct initialization, via    ConvertTo ct2 = c;      Copy construction initialization, compilation failed    ConvertTo ct3 = static_cast<convertto> (c);  Forced conversions, via    Func (c);                Copy construction initialization, compilation failed}//compilation option: g++-std=c++11 3-4-3.cpp
We have defined two types ConvertTo and convertable,convertable define a type conversion character that is explicitly converted to ConvertTo type. So for the ConvertTo type of CT variable in main, because its direct initialization is constructed in the convertable variable C, it can be compiled and passed. The ct3 of coercion type conversion is also compiled. CT2 cannot be compiled because of the need to copy constructs from C. In addition, when we use function func, the variable C passed into the convertable also causes the copy of the parameter to be constructed, and therefore cannot be compiled.

As you can see, the so-called explicit type conversion does not completely prohibit conversions from the source type to the target type, but since copy construction and non-explicit type conversions are not allowed, we usually cannot produce such a target type by assigning an expression or function argument. Conversions that are usually made through assignment expressions and function parameters are likely to be a momentary negligence of the programmer, not the intended one. With explicit type conversions, the problem is exposed, which is an important reason why we need explicit conversions.

C++11 Explicit conversion operators

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.