The Static assertions and constructor of c++11 new features delegation

Source: Internet
Author: User
Tags assert

C++11 new feature continues.
Static Assertion
Static_assert is the assertion in the compile time, the function is self-evident.
The syntax is this:

static_assertstring )   

which
Bool_constexpr: Constant expression
String: This string is a compile-time error if the bool_constexpr expression is false.

Look at the code:

// run-time assertNULL)// C++ 11// compile-time assertstatic_assert(sizeof(void4"64-bit is not supported.");

Constructor Delegation
Previously we knew that a constructor for a class could not call other constructors of this class. Each constructor can contain only the member variables and common functions of the class.

// C++03class A{    voidstd::cout"init()"; }    voidstd::cout"doSomethingElse()\n"; }public:    A() { init(); }    A(int a) { init(); doSomethingElse(); }};

But c++11 allows us to do this!

// C++11class A{    "doSomethingElse()\n"; }public:    ... }    A(int a) : A() { doSomethingElse(); }};

Then there should be a warning at the moment:
Wiki:
C++03 considers an object to being constructed when it constructor finishes executing, but C++11 considers an object Constru CTED once any constructor finishes execution. Since multiple constructors would be a allowed to execute, this would mean that each delegating constructor would be executing On a fully constructed object of the its own type. Derived class constructors would execute after all delegation in their base classes are complete.

For Base-class constructors, C++11 allows a class to specify that base class constructors would be inherited. This means the c++11 compiler would generate code to perform the inheritance, the forwarding of the derived class to T He base class. Note that this is an all-or-nothing feature; Either all of the that base class ' s constructors is forwarded or none of them are. Also, note that there is restrictions for multiple inheritance, such that class constructors cannot is inherited from Classes that use constructors with the same signature. Nor can a constructor in the derived class exist this matches a signature in the inherited base class.

Note that in c++03, non-constant data members of classes cannot is initialized at the site of the declaration of those Mem Bers. They can initialized only in a constructor. In c++11, now it ' s allowed:

// C++11class A{    99    "doSomethingElse()\n"; }public:    ... }    A(int a) : A() { doSomethingElse(); }};

Copy constructors use constructor delegation

A(const A& b): A(){  m_a = b.m_a;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The Static assertions and constructor of c++11 new features delegation

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.