-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
245 lines (187 loc) · 6.27 KB
/
Copy pathmain.cpp
File metadata and controls
245 lines (187 loc) · 6.27 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* All examples are taken from C++ Senioreas blog
* https://cppsenioreas.wordpress.com/
*/
#include <iostream>
#include <vector>
#include <numeric>
#include <type_traits>
#include <string>
#include <chrono>
class goblin {};
#if __cplusplus <= 201103L // C++11
/*
* Examples from - cpp_senioreas::the_exact_solution_for_a_generic_problem
* https://cppsenioreas.wordpress.com/2020/08/14/the-exact-solution-for-a-generic-problem-part-1/
* https://cppsenioreas.wordpress.com/2020/08/18/the-exact-solution-for-a-generic-problem-part-2/
*/
template <bool... B>
struct conjunction {};
template <bool Head, bool... Tail>
struct conjunction<Head, Tail...> : std::integral_constant<bool, Head && conjunction<Tail...>::value>{};
template <bool B>
struct conjunction<B> : std::integral_constant<bool, B> {};
template <typename T>
T sum(T t) {
return t;
}
template <typename T, typename ...Args, typename = typename std::enable_if<conjunction<std::is_arithmetic<Args>::value...>::value>::type>
typename std::common_type<T, Args...>::type sum(T t, Args ...args) {
return t + sum(args...);
}
#elif __cplusplus > 201103L && __cplusplus <= 201402L // C++14
/*
* Examples from - cpp_senioreas::the_exact_solution_for_a_generic_problem
* https://cppsenioreas.wordpress.com/2020/08/14/the-exact-solution-for-a-generic-problem-part-1/
* https://cppsenioreas.wordpress.com/2020/08/18/the-exact-solution-for-a-generic-problem-part-2/
*/
template <bool... B>
struct conjunction {};
template <bool Head, bool... Tail>
struct conjunction<Head, Tail...> : std::integral_constant<bool, Head && conjunction<Tail...>::value>{};
template <bool B>
struct conjunction<B> : std::integral_constant<bool, B> {};
template <typename T>
auto sum(T t) {
return t;
}
template <typename T, typename ...Args>
auto sum(T t, Args ...args) {
static_assert(conjunction<std::is_arithmetic<Args>::value...>::value, "All parameters should be arithmetic params.");
return t + sum(args...);
}
#elif __cplusplus > 201402L && __cplusplus <= 201703L // C++17
/*
* Examples from - cpp_senioreas::the_exact_solution_for_a_generic_problem
* https://cppsenioreas.wordpress.com/2020/08/14/the-exact-solution-for-a-generic-problem-part-1/
* https://cppsenioreas.wordpress.com/2020/08/18/the-exact-solution-for-a-generic-problem-part-2/
*/
template <typename ...Args>
typename std::enable_if_t<(std::is_arithmetic_v<Args> && ...), decltype((Args() + ...))> sum(Args ...args) {
return (args + ...);
}
/*
* Examples from - cpp_senioreas::substitution_failure_is_not_an_error_SFINAE
* https://cppsenioreas.wordpress.com/2020/08/27/substitution-failure-is-not-an-error-sfinae/
*/
class base { public: virtual void func() = 0; };
class derived : public base { public: void func() override {} };
template <typename T, typename = std::enable_if_t<std::is_base_of_v<base, T>>>
void call_func(T &&derived) {
derived.func();
}
// Simple Sum
template <typename T, typename U>
auto my_sum(T num1, U num2) {
static_assert(std::is_arithmetic_v<T> && std::is_arithmetic_v<U>, "Arguments have to be of arithmetic types.");
return num1 + num2;
}
struct my_int {
explicit my_int(int a) : val(a) {}
operator int() const { return val; }
int val;
};
template <typename T, typename U, typename = std::enable_if_t<std::is_same_v<T, my_int>>>
auto my_sum(T num1, U num2) {
return num1 + num2;
}
#else // C++20
/*
* Examples from - cpp_senioreas::the_exact_solution_for_a_generic_problem
* https://cppsenioreas.wordpress.com/2020/08/14/the-exact-solution-for-a-generic-problem-part-1/
* https://cppsenioreas.wordpress.com/2020/08/18/the-exact-solution-for-a-generic-problem-part-2/
*/
template <typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
template <Arithmetic ...Args>
constexpr auto sum(Args ...args) {
return (args + ...);
}
template <typename T>
concept LandType = std::is_arithmetic_v<T>;
namespace code_kingdom {
template <LandType T>
class land {
public:
explicit land(T t) {
vec.push_back(t);
};
void insert(T t) {
vec.push_back(t);
}
T sum() {
return std::accumulate(vec.begin(), vec.end(), T());
}
private:
std::vector<T> vec;
};
template <LandType T>
land<T> generate_land() {
return land<T>();
}
}
/*
* Examples from - cpp_senioreas::substitution_failure_is_not_an_error_SFINAE
* https://cppsenioreas.wordpress.com/2020/08/27/substitution-failure-is-not-an-error-sfinae/
*/
// Stringable
namespace std {
string to_string(string &&val) {
return val;
}
}
template <typename T>
concept Stringable = requires(T type) {
{ std::to_string(type) };
};
template <typename ...Args>
std::string chain_strings(Args ...args) {
static_assert((Stringable<Args> && ...), "Error: can chain some of those params");
}
template <Stringable ...Args>
std::string chain_strings(Args ...args) {
return (std::string() + ... + std::to_string(args));
}
// Explicit expression
struct my_goblin {
my_goblin() {}
my_goblin(int) {}
};
template <typename T>
struct my_goblin_wrapper {
explicit(!std::is_same_v<my_goblin, T>)
my_goblin_wrapper(T &&type) : base (type) {}
T &base;
};
#endif
int main() {
using namespace std::string_literals;
#if __cplusplus > 201402L && __cplusplus <= 201703L // C++17
/// Example 1
call_func(derived());
// call_func(goblin()); // Compile-time error - candidate template ignored: requirement 'std::is_base_of_v<base, goblin>' was not satisfied [with T = goblin]
/// Example 2
auto res = my_sum(1, 2.3);
// auto res1 = my_sum(my_int(3), 2.3); // An ambitious call
#endif
#if __cplusplus > 201703L
/// Example 1
// code_kingdom::land l("1"s); // Compilation error
// l.insert("2.3"s);
// l.insert("4"s);
// std::cout << l.sum() << std::endl;
code_kingdom::land l2(1.5);
l2.insert(2.3);
l2.insert(4);
std::cout << l2.sum() << std::endl;
goblin g;
//code_kingdom::land l3(g); // Compilation error
// l3.sum();
/// Example 2
std::cout << chain_strings(2, " + ", 3.5, " = ", 2 + 3.5) << std::endl;
/// Example 3
my_goblin_wrapper mgw = my_goblin();
// my_goblin_wrapper mgw1 = int(); // Without the explicit this line will compile.
#endif
return EXIT_SUCCESS;
}