-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path228.md
195 lines (155 loc) · 5.17 KB
/
228.md
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<details open><summary>Info</summary><p>
* **Did you know that C++ allows accessing private members with friend injection?**
* http://eel.is/c++draft/class.friend#:access_control,friend_function
</p></details><details open><summary>Example</summary><p>
```cpp
class foo {
private:
int data;
};
template<int foo::*Ptr>
int& get_data(foo& f) {
return f.*Ptr;
}
template<int foo::*Ptr>
struct foo_access {
friend int& get_data(foo& f) {
return f.*Ptr;
}
};
template struct foo_access<&foo::data>;
int& get_data(foo&);
int main() {
foo f{};
get_data(f) = 42; // access private data member
}
```
> https://godbolt.org/z/65zee7exf
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `get_token` which gives access to the private trade token?**
```cpp
class trade {
public:
int price{};
int size{};
constexpr auto token() const { return token_; }
private:
std::string_view token_{};
};
// TODO
std::string_view& get_token(trade&);
int main() {
using namespace boost::ut;
"access private token"_test = [] {
trade t{};
expect("" == t.token());
get_token(t) = "Quantlab";
expect("Quantlab" == t.token());
};
}
```
> https://godbolt.org/z/83dsMcErT
</p></details><details><summary>Solutions</summary><p>
```cpp
class trade {
friend std::string_view& get_token(trade&);
public:
int price{};
int size{};
constexpr auto token() const { return token_; }
private:
std::string_view token_{};
};
std::string_view& get_token(trade& t) {
return t.token_;
}
```
> https://godbolt.org/z/hvesa79n8
```cpp
class trade {
public:
int price{};
int size{};
constexpr auto token() const { return token_; }
private:
std::string_view token_{};
};
template <std::string_view trade::*Ptr>
struct trade_access {
friend auto get_token(trade& t) -> std::string_view& {
return t.*Ptr;
}
};
template struct trade_access<&trade::token_>;
auto get_token(trade& t) -> std::string_view&;
```
> https://godbolt.org/z/37aerxdEs
```cpp
template<class T, class V, V T::*Ptr, class Token>
struct BadFriend {
friend V& get_data(T& f , Token const & ) {
return f.*Ptr;
}
};
#define ACCESS( TYPE, MEMBER_TYPE, MEMBER ) \
struct MEMBER{ \
MEMBER( TYPE & t ):t(t){} \
auto & operator()(); \
TYPE & t; \
}; \
template struct BadFriend<TYPE, MEMBER_TYPE, &TYPE::MEMBER, MEMBER>; \
MEMBER_TYPE & get_data(TYPE&,MEMBER const & ); \
auto & MEMBER::operator()(){ \
return get_data( t, *this ); \
}
ACCESS(trade,std::string_view,token1_)
ACCESS(trade,std::string_view,token2_)
```
> https://godbolt.org/z/7TdcE5zGf
```cpp
namespace steal {
template <class>
struct trait;
template <class T, class U>
struct trait<T U::*> { using type = U; };
template<auto ptr, class tag>
struct member_pointer {
friend constexpr auto& get_member_pointer(
typename trait<decltype(ptr)>::type& f, const tag*) noexcept
{ return f.*ptr; }
};
}
// Must not be pasted inside any namespace
#define EXPOSE_MEMBER_AND_THEN_GO_THINK_ABOUT_WHAT_YOU_HAVE_DONE(NS, BASE, MEMBER) \
namespace NS { \
class BASE##_##MEMBER { \
BASE& base; \
public: \
constexpr BASE##_##MEMBER(BASE& base) : base(base) {} \
constexpr auto& operator()() const noexcept; \
}; \
} \
namespace steal { \
template class member_pointer<&::NS::BASE::MEMBER, ::NS::BASE##_##MEMBER>; \
constexpr auto& get_member_pointer( \
::NS::BASE&, const ::NS::BASE##_##MEMBER*) noexcept; \
} \
constexpr auto& ::NS::BASE##_##MEMBER::operator()() const noexcept { \
return ::steal::get_member_pointer(base, this); \
}
namespace business::messages {
class trade {
public:
int price{};
int size{};
constexpr auto token1() const { return token1_; }
constexpr auto token2() const { return token2_; }
private:
std::string_view token1_{};
std::string_view token2_{};
};
}
EXPOSE_MEMBER_AND_THEN_GO_THINK_ABOUT_WHAT_YOU_HAVE_DONE(business::messages, trade, token1_)
EXPOSE_MEMBER_AND_THEN_GO_THINK_ABOUT_WHAT_YOU_HAVE_DONE(business::messages, trade, token2_)
```
> https://godbolt.org/z/YWY4bv6dn