-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAttributes.cpp
More file actions
94 lines (75 loc) · 2.33 KB
/
Attributes.cpp
File metadata and controls
94 lines (75 loc) · 2.33 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
90
91
92
93
94
// =====================================================================================
// Attributes.cpp
// =====================================================================================
module modern_cpp:attributes;
// #define DEMONSTRATE_WARNINGS_AND_ERRORS 1
namespace StandardAttributes {
[[ nodiscard ]] static int discard_test()
{
return 123;
}
static void test_01()
{
int result = discard_test();
// another call to discard_test:
// 'warning: discarding return value of function with 'nodiscard' attribute'
#if defined (DEMONSTRATE_WARNINGS_AND_ERRORS)
discard_test();
#endif
}
[[ noreturn ]] static void criticalFunction() {
throw "error";
}
static void not_yet_ready()
{
// set debug mode in compiler or 'R'
[[ maybe_unused ]] char mg_brk = 'D';
// compiler does not emit any warnings
// or error on this unused variable
}
static void not_yet_ready_too()
{
// set debug mode in compiler or 'R'
char mg_brk = 'D';
}
#if defined (DEMONSTRATE_WARNINGS_AND_ERRORS)
// for class/struct/union
struct [[ deprecated ]] Struct {};
// for Functions
[[ deprecated("Reason for deprecation") ]] static void f() {}
// for namespaces
namespace [[ deprecated ]] any_namespace {}
// For variables (including static data members)
[[ deprecated ]] int x;
[[ deprecated ]] static void test_deprecated()
{
Struct s{};
f();
x = 123;
}
#endif
static void complicatedSwitch(int n)
{
switch (n)
{
case 1:
std::cout << "Digit 1 processed" << std::endl;
break;
case 2:
std::cout << "Digit 2 (partially ) processed" << std::endl;
[[ fallthrough ]]; //(dont forget the semicolon) suppressing warning
case 3:
std::cout << "And some code for Digit 2 and Digit 3" << std::endl;
break;
case -1:
std::cout << "Undefined Digit" << std::endl;
}
}
}
void main_attributes()
{
using namespace StandardAttributes;
}
// =====================================================================================
// End-of-File
// =====================================================================================