Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 2.1 KB

static-assert-macro.md

File metadata and controls

64 lines (47 loc) · 2.1 KB
description title ms.date api_location api_type topic_type f1_keywords helpviewer_keywords ms.assetid
Learn more about: _STATIC_ASSERT Macro
_STATIC_ASSERT Macro
11/04/2016
msvcrt.dll
msvcr80.dll
msvcr90.dll
msvcr100.dll
msvcr100_clr0400.dll
msvcr110.dll
msvcr110_clr0400.dll
msvcr120.dll
msvcr120_clr0400.dll
ucrtbase.dll
DLLExport
apiref
_STATIC_ASSERT
_STATIC_ASSERT macro
89b0350c-2c2f-4be6-9786-8b1f0780a5da

_STATIC_ASSERT macro

Evaluate an expression at compile time and generate an error when the result is FALSE.

Syntax

_STATIC_ASSERT(
    booleanExpression
);

Parameters

booleanExpression
Expression (including pointers) that evaluates to nonzero (TRUE) or 0 (FALSE).

Remarks

This macro resembles the _ASSERT and _ASSERTE macros, except that booleanExpression is evaluated at compile time instead of at runtime. If booleanExpression evaluates to FALSE (0), Compiler Error C2466 is generated.

Example

In this example, we check whether the sizeof an int is larger than or equal to 2 bytes and whether the sizeof a long is 1 byte. The program won't compile and it will generate Compiler Error C2466 because a long is larger than 1 byte.

// crt__static_assert.c

#include <crtdbg.h>
#include <stdio.h>

_STATIC_ASSERT(sizeof(int) >= 2);
_STATIC_ASSERT(sizeof(long) == 1);  // C2466

int main()
{
    printf("I am sure that sizeof(int) will be >= 2: %d\n",
        sizeof(int));
    printf("I am not so sure that sizeof(long) == 1: %d\n",
        sizeof(long));
}

Requirements

Macro Required header
_STATIC_ASSERT <crtdbg.h>

See also

Alphabetical function reference
_ASSERT, _ASSERTE, _ASSERT_EXPR macros