-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticAssertTest.cpp
More file actions
48 lines (43 loc) · 1.08 KB
/
Copy pathstaticAssertTest.cpp
File metadata and controls
48 lines (43 loc) · 1.08 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
//
// Created by william on 2020/12/28.
//
/*
* Syntax:语法:
* static_assert ( bool_constexpr , message ) C++11
* static_assert ( bool_constexpr ) C++17
* Explanation:解释
* 声明可以出现在命名空间和块作用域中,可以作为块声明,也可以做为成员声明
*/
#include <type_traits>
template <typename T>
void swap(T& a, T& b)
{
static_assert(std::is_copy_constructible<T>::value, "Swap requires copying");
static_assert(std::is_nothrow_copy_constructible<T>::value && std::is_nothrow_copy_assignable<T>::value, "Swap requires nothrow copy/assign");
auto c = b;
b = a;
a = c;
}
template <typename T>
struct dataStructure
{
static_assert(std::is_default_constructible<T>::value, "Data Structure requires default-constructible elements");
};
struct NoCopy
{
NoCopy(const NoCopy&) = delete;
NoCopy() = default;
};
struct NoDefault
{
NoDefault() = delete;
};
void staticAssertTest()
{
int a, b;
swap(a, b);
NoCopy ncA, ncB;
// swap(ncA, ncB);
dataStructure<int> dsOK;
// dataStructure<NoDefault> dsError;
}