"C + + note" explicit specifier

Source: Internet
Author: User

Used to suppress automatic implicit conversions of constructors.

structa{A (int) { }//Conversion ConstructorsAint,int) { }//Conversion Constructors (C++11)    operator BOOL()Const{return true; }}; structb{ExplicitBint) { }    ExplicitBint,int) { }    Explicit operator BOOL()Const{return true; }}; intMain () {A A1=1;//OK: Copy initialization selection a::a (int)A A2 (2);//OK: Direct initialization select A::A (int)A A3 {4,5};//OK: Direct list initialization Select A::A (int, int)A A4 = {4,5};//OK: Copy list initialization selection a::a (int, int)A a5 = (a)1;//OK: Explicit transformation for static_cast    if(A1);//ok:a::operator bool ()    BOOLNA1 = A1;//OK: Copy initialization selection a::operator bool ()    BOOLNa2 = static_cast<BOOL> (A1);//Direct initialization of the Ok:static_cast//B B1 = 1; //ERROR: Replication initialization does not consider b::b (int)B B2 (2);//OK: Direct initialization select B::B (int)B B3 {4,5};//OK: Direct list initialization Select B::B (int, int)//B b4 = {4, 5};//Error: Replication list initialization does not consider b::b (int,int)b b5 = (b)1;//OK: Explicit transformation for static_cast    if(B2);//ok:b::operator bool ()//bool nb1 = b2;//ERROR: Replication initialization does not consider b::operator bool ()    BOOLNB2 = static_cast<BOOL> (b2);//Direct initialization of the Ok:static_cast}

"C + + note" explicit specifier

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.