forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_assert.cpp
More file actions
28 lines (20 loc) · 730 Bytes
/
static_assert.cpp
File metadata and controls
28 lines (20 loc) · 730 Bytes
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
/*
# static_assert
Make assertions at compile time.
In this way you don't waste time compiling large programs,
or do potentially dangerous runtime operations to test your program.
Probably became possible on C++11 because of features such as `constexpr`,
which allow to better manage compile time constantness.
<http://stackoverflow.com/questions/1647895/what-does-static-assert-do-and-what-would-you-use-it-for>
*/
#include "common.hpp"
int main() {
#if __cplusplus >= 201103L
static_assert(0 < 1, "msg");
// ERROR: static assertion failed
//static_assert(0 > 1, "msg");
std::srand(time(NULL));
// ERROR: needs to be a constexpr
//static_assert(std::rand() >= 0);
#endif
}