...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
BOOST_STATIC_ASSERT
works as
follows. There is class STATIC_ASSERTION_FAILURE
which is defined as:
namespace boost{ template <bool> struct STATIC_ASSERTION_FAILURE; template <> struct STATIC_ASSERTION_FAILURE<true>{}; }
The key feature is that the error message triggered by the undefined expression
sizeof(STATIC_ASSERTION_FAILURE<0>)
, tends
to be consistent across a wide variety of compilers. The rest of the machinery
of BOOST_STATIC_ASSERT
is just
a way to feed the sizeof
expression
into a typedef
. The use of a macro
here is somewhat ugly; however boost members have spent considerable effort
trying to invent a static assert that avoided macros, all to no avail. The
general conclusion was that the good of a static assert working at namespace,
function, and class scope outweighed the ugliness of a macro.