Understanding static_assert in C++ 11 (original) (raw)

static_assert is a compile-time assertion mechanism introduced in C++11. It verifies conditions during compilation and generates an error if a condition is not satisfied.

#include using namespace std;

int main() { static_assert(sizeof(int) >= 4, "int must be at least 4 bytes");

cout << "Compilation successful";
return 0;

}

`

Output

Compilation successful

**Explanation: The assertion checks whether the size of int is at least 4 bytes. Since the condition is true, compilation succeeds.

Syntax

static_assert(constant_expression, "error message");

**where:

Compile-Time Assertions Before C++11

Before C++11, developers often used the #error directive to generate compilation errors. However, #error cannot evaluate expressions such as sizeof(), template parameters, or other compile-time computations.

CPP `

#include using namespace std; #if !defined(__geeksforgeeks) #error "Geeksforgeeks hasn't been defined yet". #endif int main() { return 0; }

`

**Output

./Solution.cpp:4:2: error: #error "Geeksforgeeks hasn't been defined yet". 4 | #error "Geeksforgeeks hasn't been defined yet". | ^~~~~

Using static_assert with Templates

One of the most common uses of static_assert is validating template arguments.

CPP `

#include using namespace std;

template <class T, int Size> class Vector { // Compile time assertion to check if // the size of the vector is greater than // 3 or not. If any vector is declared whose // size is less than 4, the assertion will fail static_assert(Size > 3, "Vector size is too small!");

T m_values[Size];

};

int main() { Vector<int, 4> four; // This will work Vector<short, 2> two; // This will fail

return 0;

}

`

**Output

error: static assertion failed: Vector size is too small!

**Explanation: The assertion ensures that the template parameter Size is greater than 3. Since v2 uses a size of 2, compilation fails.

Advantages of static_assert

The static_assert keyword provides several benefits over traditional compile-time checking techniques.

Scope of static_assert

A static_assert declaration can appear in multiple scopes within a C++ program, allowing compile-time checks to be performed at different levels of the code.

1. Namespace Scope

A static_assert declared at namespace scope is evaluated during compilation and applies to the entire translation unit.

C++ `

#include

static_assert(sizeof(void*) == 8, "64-bit architecture required");

int main() { cout << "Assertion passed"; return 0; }

`

**Output:

Assertion passed

2. Class Scope

A static_assert can be placed inside a class or class template to enforce compile-time constraints on class definitions or template parameters.CPP

C++ `

template <class T, int Size> class Vector { static_assert(Size > 3, "Vector size is too small");

T values[Size];

};

`

3. Block Scope

A static_assert can be declared inside a function or block to validate compile-time conditions that are relevant to that specific scope.

C++ `

template <typename T, int N> void func() { static_assert(N >= 0, "Array size cannot be negative");

T arr[N];

}

`

Invalid static_assert Expressions

The condition supplied to static_assert must be a valid compile-time constant expression.

C++ `

// CPP program to illustrate // demonstrating an erroneous static_assert declaration int main() { static_assert(1 / 0, "never shows up!"); return 0; }

`

**Output

prog.cpp:5:2: error: non-constant condition for static assertion static_assert(1 / 0, "never shows up!"); ^

**Explanation: The expression 1 / 0 is not a valid constant expression. Therefore, the compiler reports an error before evaluating the assertion message.