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