C++11 Static_assert

Source: Internet
Author: User
Tags assert

The keyword static_assert is introduced in c++0x, which is used to make assertions during compilation, so it is called a static assertion.

Its syntax:static_assert(constant expression, hint-string).

If the value of the first parameter constant expression is False, a compilation error is generated, where the error location is the row of the Static_assert statement, and the second parameter is the error prompt string.

With Static_assert, we can find more errors during compilation, use a compiler to enforce some contracts, and help us improve the readability of compiled information, especially when it comes to templates.

Static_assert can be used in a global scope, in a namespace, in a class scope, in a function scope, with almost unlimited use.

When the compiler encounters a static_assert statement, it usually calculates its first argument as a constant expression immediately, but if the constant expression relies on some template arguments, then the calculation is deferred until the template is instantiated, which makes it possible to check the template parameters.

Since the concepts proposal, which was expected to join the C++0X standard, was eventually rejected, it is up to the Static_assert to check that the template parameters meet the desired task, so how to construct the appropriate constant expression, will be a topic worthy of discussion.

In terms of performance, the Static_assert does not cause any run-time performance penalty because it is an assertion during Static_assert compilation and does not generate the target code.

Simple example:

Static_assert (sizeof(void4"64-bit code generation is not supported. ) ");

The static_assert is used to ensure that the compilation takes place only on a 32-bit platform and does not support a 64-bit platform, which can be placed at the beginning of the file so that it can be checked early to save compile time in case of failure.

Another example:

#include <cassert>#include<cstring>using namespacestd;template<typename T, TypeName u>intBit_copy (t& A, u&b) {Assert (sizeof(b) = =sizeof(a));
  //static_assert (sizeof (b) = = sizeof (a), "template parameter Size no equal!"); memcpy (&a,&b,sizeof(b));};intMain () {intAAA =0x2468; DoubleBBB; Bit_copy (AAA, BBB); GetChar (); return 0;}

The Assert runtime assertion is used here, but if Bit_copy is not called, we will not be able to trigger the assertion, and the timing of the assertion should actually be generated when the template is instantiated, that is, the compile time.

Compile again with Static_assert replace assert to get an error and display the error message we specified.

Note: The result of an assertion expression of static_assert must be an expression that can be evaluated at compile time, that is, a constant expression. If a variable is used, it causes an error.

int positive (constint  n) {    0"value must > 0 " );     return 0 ;}

and Assert, #error比较

We know that the existing standard for C + + has an assert, #error two facilities, is also used to check for errors, and some third-party static assertion implementations.

assert is a run-time assertion, it is used to find errors during the run, not to the compile time to find errors, nor mandatory, nor to improve the readability of the compilation information, since it is run-time check, of course, has an impact on performance, so often in the release version, The assert will be turned off;

#error can be seen as a pre-compilation assertion, not even an assertion, can only display an error message at the time of precompilation, it can do little, it can cooperate with #ifdef/ifndef to participate in the pre-compilation condition check, because it cannot get compile information, Of course, there is no further analysis.

Before Stastic_assert submitted to the C++0X standard, in order to compensate for the assert and #error deficiencies, there are some third-party solutions that can be used for the compile-time static check, for example: Boost_static_assert and Loki_static_check, but since they are all using some of the compiler's cryptic features to implement the trick, portability, simplicity is not too good, but also reduce the compilation speed, And the function is not perfect, for example Boost_static_assert can not define the error message text, while Loki_static_check requires prompt text to satisfy the C + + type definition syntax.

C++11 Static_assert

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.