- Example
In this example, we will look at the usage of static_assert.
We'll cover the following...
Example
Press + to interact
// staticAssert.cpp#include <iostream>#include <type_traits>template< class T >struct Add{// check the assertionstatic_assert(std::is_arithmetic<T>::value, "Argument T must be an arithmetic type");} ;int main(){// will workstatic_assert(sizeof(void*) >= 8, "64-bit addressing is required for this program");// int is arithmeticAdd<int> addInt= Add<int>();// double is arithmeticAdd<double> addDouble= Add<double>();// char is arithmeticAdd<char> addChar= Add<char>();// std::string is not arithmeticAdd<std::string> addString= Add<std::string>(); // if you comment this line, the code will run fine}
Explanation
The program uses ...