-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path302.md
115 lines (84 loc) · 2.72 KB
/
302.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
<details open><summary>Info</summary><p>
* **Did you now that with concepts you can override a type?**
* https://eel.is/c++draft/temp.concept#def:concept
</p></details><details open><summary>Example</summary><p>
```cpp
template<auto N> struct foo {
static constexpr auto value = N;
};
static_assert(42 == foo<42>::value);
template<auto N>
requires true // override -> more specialized
struct foo<N> {
static constexpr auto value = 0;
};
static_assert(42 == foo<42>::value); // memoized
static_assert(0 == foo<43>::value);
```
> https://godbolt.org/z/hPcsKG4an
</p></details><details open><summary>Puzzle</summary><p>
* **Can you override std::shared_ptr to avoid thred-safe guards?**
```cpp
#include <memory>
// TODO override shared_ptr with std::__shared_ptr<int, __gnu_cxx::_S_single> which is is not thread-safe
// NOTE overriding STL is UB
// Alternative - boost::local_shared_ptr
#include <type_traits>
static_assert(std::is_base_of_v<std::__shared_ptr<int, __gnu_cxx::_S_single>, std::shared_ptr<int>>);
```
> https://godbolt.org/z/7axP5or3q
</p></details><details><summary>Solutions</summary><p>
```cpp
namespace std {
template<class T> requires ::std::is_same_v<T, int>
class shared_ptr<T> : public __shared_ptr<int, __gnu_cxx::_S_single> {};
}
```
> https://godbolt.org/z/Mhj1M7Yec
```cpp
namespace std {
template <class T>
requires std::is_integral_v<T>
class shared_ptr<T> : public __shared_ptr<int, __gnu_cxx::_S_single> {};
} // namespace std
```
> https://godbolt.org/z/sa9a937of
```cpp
namespace boost{
template<typename N> requires std::is_same_v<int,N>
struct shared_ptr<N> : std::__shared_ptr<N, __gnu_cxx::_S_single> {
};
}
static_assert(std::is_base_of_v<std::__shared_ptr<int, __gnu_cxx::_S_single>, boost::shared_ptr<int>>);
```
> https://godbolt.org/z/acWKbG4fG
```cpp
namespace std{
template<typename N> requires is_same_v<int,N>
struct shared_ptr<N> : std::__shared_ptr<N, __gnu_cxx::_S_single> {
};
}
static_assert(std::is_base_of_v<std::__shared_ptr<int, __gnu_cxx::_S_single>, std::shared_ptr<int>>);
```
> https://godbolt.org/z/z7YznT45n
```cpp
template <class T>
concept integral = std::is_integral_v<T>;
namespace std{
template<integral T>
class shared_ptr<T> : public std::__shared_ptr<T, __gnu_cxx::_S_single>{
};
}
#include <type_traits>
static_assert(std::is_base_of_v<std::__shared_ptr<int, __gnu_cxx::_S_single>, std::shared_ptr<int>>);
```
> https://godbolt.org/z/PP6KEczMb
```cpp
template<typename T>
requires true
class std::shared_ptr<T> : std::__shared_ptr<T, __gnu_cxx::_S_single> {
};
#include <type_traits>
static_assert(std::is_base_of_v<std::__shared_ptr<int, __gnu_cxx::_S_single>, std::shared_ptr<int>>);
```
> https://godbolt.org/z/rrr8GP3qe