forked from chronolaw/boost_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathref.cpp
More file actions
151 lines (119 loc) · 2.73 KB
/
Copy pathref.cpp
File metadata and controls
151 lines (119 loc) · 2.73 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2015
// Author: Chrono Law
#include <cmath>
#include <functional>
#include <std.hpp>
//using namespace std;
#include <boost/ref.hpp>
using namespace boost;
struct square
{
typedef void result_type;
result_type operator()(int &x)
{ x = x * x; }
};
//////////////////////////////////////////
void case1()
{
std::vector<int> v = {1,2,3,4,5};
for_each(v.begin(), v.end(), square());
}
//////////////////////////////////////////
void case2()
{
int x = 10;
reference_wrapper<int> rw(x);
assert(x == rw);
(int &)rw = 100;
assert(x == 100);
reference_wrapper<int> rw2(rw);
assert(rw2.get() == 100);
std::string str;
reference_wrapper<std::string> rws(str);
*rws.get_pointer() = "test reference_wrapper";
std::cout << rws.get().size() << std::endl;
}
//////////////////////////////////////////
void case3()
{
double x = 2.71828;
auto rw = cref(x);
std::cout << typeid(rw).name() << std::endl;
std::string str;
auto rws = boost::ref(str);
std::cout << typeid(rws).name() << std::endl;
boost::cref(str); //adl
std::cout << std::sqrt(ref(x)) << std::endl; //计算平方根
}
//////////////////////////////////////////
void case4()
{
std::vector<int> v(10, 2) ;
auto rw = boost::cref(v);
assert( is_reference_wrapper<decltype(rw)>::value);
assert(!is_reference_wrapper<decltype(v)>::value);
std::string str;
auto rws = boost::ref(str);
std::cout << typeid(unwrap_reference<decltype(rws)>::type).name() << std::endl;
std::cout << typeid(unwrap_reference<decltype(str)>::type).name() << std::endl;
}
//////////////////////////////////////////
void case5()
{
std::set<int> s ;
auto rw = boost::ref(s);
unwrap_ref(rw).insert(12);
std::string str("test");
auto rws = boost::cref(str);
std::cout << unwrap_ref(rws) << std::endl;
std::cout << unwrap_ref(str) << std::endl;
}
//////////////////////////////////////////
class big_class
{
private:
int x;
public:
big_class():x(0){}
void print()
{ std::cout << "big_class " << ++x << std::endl; }
};
template<typename T>
void print(T a)
{
for (int i = 0;i < 2; ++i)
unwrap_ref(a).print();
}
void case6()
{
big_class c;
auto rw = ref(c);
c.print();
print(c);
print(rw);
print(c);
c.print();
}
void case7()
{
using namespace std;
typedef double (*pfunc)(double);
pfunc pf = sqrt;
cout << std::ref(pf)(5.0) << endl;
square sq;
int x =5;
std::ref(sq)(x);
cout << x << endl;
vector<int> v = {1,2,3,4,5};
for_each(v.begin(), v.end(), std::ref(sq));
}
int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
}