aboutsummaryrefslogtreecommitdiffstats
path: root/find.hpp
blob: edba06dda2e7ce7809d72ee19afc264739e91d55 (plain)
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
#ifndef MBUOY_FIND_HPP
#define MBUOY_FIND_HPP

#include <optional>
#include <type_traits>

namespace mbuoy {

template<typename T, typename U>
concept can_static_cast = requires(U u) {
    static_cast<T>(u);
};

template<typename T>
consteval std::optional<T> find(T val)
{
    return val;
}

template<typename T, typename U>
    requires (!std::is_same_v<T, U> && can_static_cast<T, U>)
consteval std::optional<U> find(U val)
{
    return val;
}

template<typename T, typename U>
    requires (!std::is_same_v<T, U> && !can_static_cast<T, U>)
consteval std::optional<T> find(U val)
{
    return {};
}

template<typename T>
consteval std::optional<T> find(T val, auto... vals)
{
    return val;
}

template<typename T, typename U>
    requires (!std::is_same_v<T, U> && can_static_cast<T, U>)
consteval std::optional<U> find(U val, auto... vals)
{
    return val;
}

template<typename T, typename U>
    requires (!std::is_same_v<T, U> && !can_static_cast<T, U>)
consteval auto find(U val, auto... vals)
{
    return find<T>(vals...);
}

} // namespace mbuoy

#endif // MBUOY_FIND_HPP