Simple implementation of boost static assertions

Source: Internet
Author: User

Boost's static assertions enable the compiler to detect incorrect types or syntax errors, reduce possible errors during runtime, and take full advantage of the advantages provided by the compiler. The core implementation of static assertions is to declare not to implement only through the template's special features. The following is a simple static assertion I have implemented.


Template <bool>

Struct static_assert;


Template <>

Struct static_assert <true> {};

The class implementation in the true mode is implemented here, but the class implementation in the false mode is not implemented. Therefore, if the condition is false, a variable declared as static_assert will certainly fail to be compiled.

Defining a static_assert variable completes condition judgment.

Static_assert <cond> ();


Here we define a macro to simplify this job.

# Define static_assert (Cond )\

(Void) static_assert <cond> ()


So the final result is

Static_assert (0 = 0 );

Static_assert (1 = 0); // compilation failed




Simple implementation of boost static assertions

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.