-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStaticAssert.cpp
More file actions
46 lines (35 loc) · 1.63 KB
/
Copy pathStaticAssert.cpp
File metadata and controls
46 lines (35 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// =====================================================================================
// StaticAssert.cpp // static_assert keyword
// =====================================================================================
module modern_cpp:static_assert_keyword;
namespace StaticAssertDemo {
template < typename T, int Height, int Width >
struct Grid {
static_assert(Height >= 0, "Height must be positive.");
static_assert(Width >= 0, "Width must be positive.");
static_assert(Height + Width > 0, "Height and Width must be greater than 0.");
};
template <class T, int Size>
class MyVector {
// 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];
};
}
void main_static_assert()
{
using namespace StaticAssertDemo;
//static_assert(sizeof(void*) == 4, "Size of Pointer is 32-bits!");
static_assert(sizeof(void*) == 8, "Size of Pointer is 64-bits!");
[[ maybe_unused]] Grid<int, 10, 5> intGrid;
[[ maybe_unused]] Grid<std::string, 3, 4> stringGrid;
// Grid<double, 0, 0> doubleGrid; // doesn't compile
[[ maybe_unused]] Grid<long long, 1, 0> longGrid;
[[ maybe_unused]] MyVector<int, 4> four; // compiles
// MyVector<short, 2> two; // doesn't compile
}
// =====================================================================================
// End-of-File
// =====================================================================================