STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Macros for Checking Assertions

There are quite a bunch of macros for testing assertions. You must be careful to pick the right one depending on when and what you want to assert on.

Compile-time Assertions: STXXL_STATIC_ASSERT

To check specific conditions at compile time use STXXL_STATIC_ASSERT.

struct item { int a,b,c,d; }
STXXL_STATIC_ASSERT(sizeof(item) == 4 * sizeof(int));

Assertions in Unit Tests: STXXL_CHECK

Assertions in unit tests must use the following macros to ensure that the condition is also checked in release builds (where a plain "assert()" is void). These CHECK function should NOT be used to test return values, since we try to throw exceptions instead of aborting the program.

// test a condition
STXXL_CHECK( 2+2 == 4 );
// test a condition and output a more verbose reason on failure
STXXL_CHECK2( 2+2 == 4, "We cannot count!");

Sometimes one also wants to check that a specific expression throws an exception. This checking can be done automatically using a try { } catch {} by using STXXL_CHECK_THROW.

Plain Assertions: assert

For the usual assertions, that should be removed in production code for performance, we use the standard "assert()" function.

However, there is also STXXL_ASSERT(), which can be used as a replacement for assert(), when compiler warnings about unused variables or typedefs occur. The issue is that assert() completely removes the code, whereas STXXL_ASSERT() keeps the code encloses it inside if(0).