We recommend using Visual Studio 2017

_STATIC_ASSERT Macro

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at _STATIC_ASSERT Macro.

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

_STATIC_ASSERT(  
    booleanExpression  
);  

Parameters

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

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.

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 will not 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));  
}  

MacroRequired header
_STATIC_ASSERT<crtdbg.h>

System::Diagnostics::Debug::Assert

Alphabetical Function Reference
_ASSERT, _ASSERTE, _ASSERT_EXPR Macros

Show: