Repository navigation
Expand file tree
/
Copy pathnoexceptTest.cpp
More file actions
90 lines (71 loc) · 2.43 KB
/
Copy pathnoexceptTest.cpp
File metadata and controls
90 lines (71 loc) · 2.43 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <vector>
//This is included in the sample code in noexcept.html (specified below), but doesn't seem actually necessary:
//#include <utility>
//Bad habit:
//using namespace std;
/*
# To compile:
# most basic:
g++ -std=c++11 -o noexceptTest.o noexceptTest.cpp
# without -o, outputs to default a.out
# without -std=c++11 (or 14, or 17...), 2 warnings and 12 errors, such as:
1- error: expected function body after function declarator
void no_throw() noexcept;
2- warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto lmay_throw = []{};
3- error: expected expression
auto lmay_throw = []{};
4- error: use of undeclared identifier 'noexcept'
# To run:
./noexceptTest.o
From https://en.cppreference.com/w/cpp/language/noexcept.html
My contributions:
1- removing apparently unnecessary #include <utility>
2- specifying how to build successfully
3- specifying output in comments
*/
void may_throw();
void no_throw() noexcept;
auto lmay_throw = []{};
auto lno_throw = []() noexcept {};
class T
{
public:
~T(){} // dtor prevents move ctor
// copy ctor is noexcept
};
class U
{
public:
~U(){} // dtor prevents move ctor
// copy ctor is noexcept(false)
std::vector<int> v;
};
class V
{
public:
std::vector<int> v;
};
int main()
{
T t;
U u;
V v;
std::cout << std::boolalpha <<
"may_throw() is noexcept(" << noexcept(may_throw()) << ")\n" // false
"no_throw() is noexcept(" << noexcept(no_throw()) << ")\n" // true
"lmay_throw() is noexcept(" << noexcept(lmay_throw()) << ")\n" // false
"lno_throw() is noexcept(" << noexcept(lno_throw()) << ")\n" // true
"~T() is noexcept(" << noexcept(std::declval<T>().~T()) << ")\n" // true
// note: the following tests also require that ~T() is noexcept because
// the expression within noexcept constructs and destroys a temporary
"T(rvalue T) is noexcept(" << noexcept(T(std::declval<T>())) << ")\n" // true
"T(lvalue T) is noexcept(" << noexcept(T(t)) << ")\n" // true
"U(rvalue U) is noexcept(" << noexcept(U(std::declval<U>())) << ")\n" // false
"U(lvalue U) is noexcept(" << noexcept(U(u)) << ")\n" // false
"V(rvalue V) is noexcept(" << noexcept(V(std::declval<V>())) << ")\n" // true
"V(lvalue V) is noexcept(" << noexcept(V(v)) << ")\n"; // false
std::cout << "\nDone\n";
return 0;
}