-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.assert.cpp
More file actions
69 lines (59 loc) · 1.19 KB
/
1.assert.cpp
File metadata and controls
69 lines (59 loc) · 1.19 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by satellite on 2023-04-06.
//
#include "assert.h" //断言assert所在头文件
#include "iostream"
#include "cstring"
//assert断言
int* ArrAlloc(int n)
{
assert(n > 0);
return new int[n];
}
int main()
{
ArrAlloc(0);
}
/*
//枚举编译器对各种特性的支持,每个枚举值占一位
enum FeatureSupports{
C99=0x0001,
ExtInt=0x0002,
SAssert=0x0004,
NoExcept=0x0008,
SMAX=0x0010,
};
//一个编译器类型,包括名称、特性支持等
struct Compiler{
const char*name;
int spp;//使用FeatureSupports枚举
};
int main(){
//检查枚举值是否完备
assert((SMAX-1)==(C99|ExtInt|SAssert|NoExcept));
//assert为运行时断言
Compiler a={"abc",(C99|SAssert)};
//...
if(a.spp & C99){
//一些代码...
}
}
*/
//静态断言: static_assert(返回bool类型的表达式,用于说明断言错误的字符串)
/*
template<typename t,typename u>
int bit_copy(t&a,u&b)
{
static_assert(sizeof(b)==sizeof(a),"the parameters of bit_copymust havesame width.");
};
int main(){
int a=0x2468;
double b;
bit_copy(a,b);
}
*/
int positive(const int n)
{
const int m = 20;
static_assert(m>0,"value must>0");
}