-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstatic_assert.cpp
More file actions
55 lines (43 loc) · 1.29 KB
/
static_assert.cpp
File metadata and controls
55 lines (43 loc) · 1.29 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
47
48
49
50
51
52
53
54
55
//*****************************************************************************
//
// Author: Michael Price
// License: Attribution-NonCommercial-NoDerivs 3.0 Unported
// http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode
//
//*****************************************************************************
#ifndef WIN32
namespace std {
template <typename T>
struct is_integral { static const bool value; };
template <>
struct is_integral<int> { static const bool value; };
template <typename T>
const bool is_integral<T>::value = false;
const bool is_integral<int>::value = true;
void memcpy(void*, void*, long long);
}
#endif // WIN32
template <typename T>
void some_integral_stuff (T && t)
{
static_assert(std::is_integral<T>::value,
"Template param is not an integral type");
}
struct StuffThis
{
char one;
char two;
char three;
char four;
//char five; // Added in most recent build!
};
void my_stuff ()
{
some_integral_stuff(1);
//some_integral_stuff(1.0); // ERROR!!!
StuffThis st;
char buffer[4];
static_assert(sizeof(StuffThis) <= sizeof(buffer),
"Uh oh, our buffer is not big enough... what changed!");
std::memcpy(buffer, &st, sizeof(st)); // Would overflow buffer!
}