C_ASSERT Macro

Checks assertions at compile time.

Syntax

C++
void C_ASSERT(
     expr
);

Parameters

expr

An expression that can be determined at compile time.

Return Value

If the assertion succeeds, the expression evaluates to typedef char __C_ASSERT__[1];, which the compiler will accept.

If the assertion fails, the expression evalues to typedef char __C_ASSERT__[-1];, which results in a compilation error because negative subscripts are not valid.

Remarks

The C_ASSERT macro is defined as follows.

#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]

The following examples demonstrate common types of compile-time assertions.

C_ASSSERT (BUFFER_CCH_SIZE <= MAX_PATH);

C_ASSERT (ARRAYSIZE(array1) == ARRAYSIZE(array2));

C_ASSERT (FIELD_OFFSET(STRUCT_DEF, MemberName) == 0x1d4);

C_ASSERT (sizeof(BOOLEAN) == sizeof(UCHAR));

Requirements

HeaderWinnt.h

Send comments about this topic to Microsoft

Build date: 7/30/2009

Tags : c_assert macro


Page view tracker