M5Utility 0.0.12 git rev:13bfbd6
Loading...
Searching...
No Matches
extension.hpp
Go to the documentation of this file.
1
11#ifndef M5_UTILITY_STL_EXTENSION_HPP
12#define M5_UTILITY_STL_EXTENSION_HPP
13
14#include <algorithm>
15#include <cstddef>
16#include <type_traits>
17
18namespace m5 {
19namespace stl {
20
22template <class C>
23constexpr auto size(const C& c) -> decltype(c.size())
24{
25 return c.size();
26}
27
29template <typename T, size_t N>
30constexpr auto size(const T (&)[N]) noexcept -> size_t
31{
32 return N;
33}
34
43#if __cplusplus >= 201703L
44using std::clamp;
45#else
46template <class T>
47constexpr const T& clamp(const T& v, const T& low, const T& high)
48{
49 return (v < low) ? low : (high < v) ? high : v;
50}
51
52template <class T, class Compare>
53constexpr const T& clamp(const T& v, const T& low, const T& high, Compare comp)
54{
55 return comp(v, low) ? low : comp(high, v) ? high : v;
56}
57#endif
58
66template <typename E>
67constexpr inline typename std::underlying_type<E>::type to_underlying(const E e) noexcept
68{
69 return static_cast<typename std::underlying_type<E>::type>(e);
70}
71
72} // namespace stl
73} // namespace m5
74
75#endif
constexpr auto size(const C &c) -> decltype(c.size())
Like std::size C++17 or later.(for container)
Definition extension.hpp:23
constexpr const T & clamp(const T &v, const T &low, const T &high)
Like std::clamp C++17 or later.
Definition extension.hpp:47
constexpr std::underlying_type< E >::type to_underlying(const E e) noexcept
Converts an enumeration to its underlying type.(Like std::to_underlying C++23 or later)
Definition extension.hpp:67
Top level namespace of M5.
Definition base64.cpp:39
STL compatibility functions and classes.