-
Notifications
You must be signed in to change notification settings - Fork 6
/
demo-in-2.cpp
63 lines (48 loc) · 1.83 KB
/
demo-in-2.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
#include <https://raw.githubusercontent.com/hsutter/misc/master/hst.h>
#include <string>
#include <iostream>
void copy_from(auto...) { }
using String = hst::noisy<std::string>;
//------------------------------------------------------------------------------
// Today's "old" in-parameter implementation -- simple -- one parameter
//------------------------------------------------------------------------------
void old_in(const String& s) {
copy_from(s);
}
void old_in(String&& s) {
copy_from(std::move(s));
}
//------------------------------------------------------------------------------
// Proposed "new" in-parameter implementation -- simple -- one parameter
//------------------------------------------------------------------------------
void new_in(in String s) {
copy_from(s);
}
//------------------------------------------------------------------------------
//
// Compare current and proposed "in" parameter styles... both implement this:
//
// void f( /*in String s */ ) {
// //...
// copy_from(s);
// //...
// }
//
// where "old_in" does it today's way, and "new_in" uses an "in" parameter.
//
//------------------------------------------------------------------------------
void compare(auto name, auto f1, auto f2) {
std::cout << name << "\n old: " << hst::run_history(f1)
<< "\n new: " << hst::run_history(f2) << "\n\n";
}
int main() {
compare("nontrivial lvalue",
[]{ String x; old_in(x); },
[]{ String x; new_in(x); });
compare("nontrivial xvalue",
[]{ String x; old_in(std::move(x)); },
[]{ String x; new_in(std::move(x)); });
compare("nontrivial prvalue",
[]{ old_in(String()); },
[]{ new_in(String()); });
}