You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.1 KiB
C++

#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