-
Notifications
You must be signed in to change notification settings - Fork 8
/
1_cpp14.cpp
87 lines (76 loc) · 1.51 KB
/
1_cpp14.cpp
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
#include <type_traits>
#include <utility>
#include <vrm/core/static_if.hpp>
struct Cat
{
Cat() = delete;
void meow() const
{
std::cout << "meow\n";
}
};
struct Dog
{
Dog() = delete;
void bark() const
{
std::cout << "bark\n";
}
};
template <typename...>
using void_t = void;
template <typename, typename = void>
struct is_callable : std::false_type
{
};
template <typename TF, class... TArgs>
struct is_callable<TF(TArgs...),
void_t<decltype(std::declval<TF>()(std::declval<TArgs>()...))>>
: std::true_type
{
};
template <typename TF>
struct validity_checker
{
template <typename... Ts>
constexpr auto operator()(Ts&&...) const
{
return is_callable<TF(Ts...)>{};
}
};
template <typename TF>
constexpr auto is_valid(TF)
{
return validity_checker<TF>{};
}
template <typename T>
auto make_noise(const T& x)
{
auto has_meow = is_valid([](auto&& x) -> decltype(x.meow())
{
});
auto has_bark = is_valid([](auto&& x) -> decltype(x.bark())
{
});
vrm::core::static_if(has_meow(x))
.then([](const auto& y)
{
y.meow();
})
.else_if(has_bark(x))
.then([](const auto& y)
{
y.bark();
})
.else_([](const auto&)
{
struct cannot_meow_or_bark;
cannot_meow_or_bark{};
})(x);
}
int main()
{
make_noise(Cat{});
make_noise(Dog{});
// make_noise(int{});
}