-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoexceptSpecifier.cpp
87 lines (67 loc) · 2.09 KB
/
NoexceptSpecifier.cpp
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
/**
* \file NoexceptSpecifier.cpp
* \brief noexcept specifier
*
* \see https://en.cppreference.com/w/cpp/language/noexcept
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
void foo() noexcept(true)
{
STD_TRACE_FUNC;
try {
std::string str = "xxxxx";
str.at(10); // Compile Time - OK
// static_assert(noexcept(str.at(10))); // Compile Time - error: static assertion failed
static_assert(!noexcept(str.at(0))); // Compile Time - OK
std::cout << STD_TRACE_VAR(str) << std::endl;
throw 1; // Compile Time - OK
}
catch (const std::exception &a_e) {
std::cout << STD_TRACE_VAR(a_e.what()) << std::endl;
}
catch (...) {
std::cout << "Unknown" << std::endl;
}
}
//--------------------------------------------------------------------------------------------------
void foo2() noexcept(false)
{
STD_TRACE_FUNC;
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
{
static_assert(noexcept(true)); // Compile Time - OK
static_assert(noexcept(false)); // Compile Time - OK
}
{
if ( noexcept(true) ) {
std::cout << "noexcept - true" << std::endl;
}
if ( noexcept(false) ) {
std::cout << "noexcept - false" << std::endl;
}
std::cout << std::endl;
}
{
const bool is_foo_except = noexcept( ::foo() );
static_assert(is_foo_except); // Compile Time - OK
std::cout << STD_TRACE_VAR(is_foo_except) << std::endl;
std::cout << std::endl;
}
// static_assert( noexcept(::foo2()) ); // error: static assertion failed
::foo(); // Compile Time - OK
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
noexcept - true
noexcept - false
is_foo_except: 1
::: foo :::
a_e.what(): basic_string::at: __n (which is 10) >= this->size() (which is 5)
#endif