forked from hsutter/cppfront
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpure2-function-typeids.cpp2
More file actions
102 lines (72 loc) · 3.54 KB
/
Copy pathpure2-function-typeids.cpp2
File metadata and controls
102 lines (72 loc) · 3.54 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
95
96
97
98
99
100
101
102
// --- Scaffolding
f: () = { std::cout << "hello world!\n"; }
g_in : ( s: std::string) = { std::cout << "Come in, (s)$\n"; }
g_inout: (inout s: std::string) = { std::cout << "Come in awhile, but take some biscuits on your way out, (s)$!\n"; }
g_out : (out s: std::string) = { s = "A Powerful Mage"; }
g_move : (move s: std::string) = { std::cout << "I hear you've moving, (s)$?\n"; }
h_forward: (inout s: std::string) -> forward std::string = { std::cout << "Inout (s)$ ... "; return s; }
h_out : ( s: std::string) -> std::string = { std::cout << "In (s)$ ... "; return "yohoho"; }
f1: (a: std::function< (x:int) -> int >) -> int = a(1);
f2: (a: * (x:int) -> int ) -> int = a(2);
g : (x:int) -> int = x+42;
// --- Tests for type aliases
A_h_forward: type == (inout s: std::string) -> forward std::string;
main: () =
{
// --- Test basic/degenerate cases
// Test std::function< void() >
ff: std::function< () -> void > = f&;
ff();
// Ordinary pointer to function, deduced (always worked)
pf: * () -> void = f&;
pf();
// --- Tests for parameters
// Note: Not forward parameters which imply a template...
// function type-ids are for single function signatures
fg_in : std::function< ( s: std::string) -> void > = g_in&;
fg_inout: std::function< (inout s: std::string) -> void > = g_inout&;
fg_out : std::function< (out s: std::string) -> void > = g_out&;
fg_move : std::function< (move s: std::string) -> void > = g_move&;
pg_in : * ( s: std::string) -> void = g_in&;
pg_inout: * (inout s: std::string) -> void = g_inout&;
pg_out : * (out s: std::string) -> void = g_out&;
pg_move : * (move s: std::string) -> void = g_move&;
frodo: std::string = "Frodo";
sam : std::string = "Sam";
// Test in param
fg_in(frodo);
pg_in(sam);
// Test inout
fg_inout(frodo);
pg_inout(sam);
// Test out
gandalf : std::string;
galadriel: std::string;
fg_out(out gandalf);
std::cout << "fg_out initialized gandalf to: (gandalf)$\n";
pg_out(out galadriel);
std::cout << "pg_out initialized galadriel to: (galadriel)$\n";
gandalf = "Gandalf";
galadriel = "Galadriel";
// Test move
fg_move(frodo); // last use, so (move frodo) is not required
pg_move(sam); // last use, so (move sam) is not required
// --- Tests for single anonymous returns
// Note: Not multiple named return values... function-type-ids
// are for Cpp1-style (single anonymous, possibly void) returns
fh_forward: std::function< (inout s: std::string) -> forward std::string > = h_forward&;
fh_out : std::function< ( s: std::string) -> std::string > = h_out&;
ph_forward: * (inout s: std::string) -> forward std::string = h_forward&;
ph_out : * ( s: std::string) -> std::string = h_out&;
ph_forward2: * A_h_forward = h_forward&;
// Test forward return
std::cout << "fh_forward returned: (fh_forward(gandalf))$\n";
std::cout << "ph_forward returned: (ph_forward(galadriel))$\n";
std::cout << "ph_forward2 returned: (ph_forward2(galadriel))$\n";
// Test out return
std::cout << "fh_out returned: (fh_out(gandalf))$\n";
std::cout << "ph_out returned: (ph_out(galadriel))$\n";
// --- Tests for function parameters
std::cout << "(f1(g&))$\n";
std::cout << "(f2(g&))$\n";
}