- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
#include <optional>
namespace aim {
template <typename T, template <typename> typename Cont>
struct is_valid {};
template <typename T, template <typename> typename Cont>
struct chain;
template <typename T, template <typename> typename Cont>
struct chain {
using value_type_t = T;
using holder_t = Cont<value_type_t>;
chain()
{}
chain(holder_t&& value_)
: value(std::move(value_))
{}
template <typename Func>
constexpr auto otherwise(Func&& func) {
return chain<T, Cont>{std::move(func())};
}
template <typename Func>
constexpr auto with(Func&& func) -> chain<value_type_t, Cont> {
if (bool(value)) {
return chain<value_type_t, Cont>{Cont<value_type_t>{func(*value)}};
}
else {
return chain<value_type_t, Cont>{};
}
}
holder_t value;
};
template <typename T, template <typename> typename Cont>
chain(Cont<T>&&) -> chain<T, Cont>;
template <typename T, template <typename> typename Cont>
struct use {
using value_type_t = T;
using holder_t = Cont<value_type_t>;
use(holder_t&& value)
: value(std::move(value))
{}
template <typename Func>
constexpr auto with(Func&& func) -> chain<value_type_t, Cont> {
if (is_valid<T, Cont>{}(value)) {
return chain<value_type_t, Cont>{Cont<value_type_t>{func(*value)}};
}
else {
return chain<value_type_t, Cont>{};
}
}
holder_t value;
};
template <typename T, template <typename> typename Cont>
use(Cont<T>&&) -> use<T, Cont>;
}
namespace aim {
template <typename T>
struct is_valid<T, std::optional> {
constexpr bool operator()(std::optional<T> const& value) {
return bool(value);
}
};
}
#include <iostream>
#include <memory>
int main() {
{
std::optional<int> a;
aim::use(std::move(a)).with([](auto v) {
std::cout << v << '\n';
return 1;
}).otherwise([](){
std::cout << "it's none!\n";
return 2;
}).with([](auto v){
std::cout << v << '\n';
return 3;
});
}
}
j123123 06.07.2017 18:47 # 0
j123123 06.07.2017 19:20 # +1
gost 09.07.2017 12:58 # 0
sos 07.07.2017 01:39 # +1
Гости могут высказаться только в хуй, пизду и джигурду.
j123123 07.07.2017 02:01 # 0
inkanus-gray 08.07.2017 03:08 # 0
Antervis 07.07.2017 06:10 # 0