MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
icommon.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_INTERFACE_ICOMMON_HPP__
2#define MSTL_CORE_INTERFACE_ICOMMON_HPP__
3
11
14
20
29template <typename T>
30struct ihashable {
31private:
32 constexpr const T& derived() const noexcept {
33 return static_cast<const T&>(*this);
34 }
35
36public:
43 MSTL_NODISCARD constexpr size_t to_hash() const
44 noexcept(noexcept(derived().to_hash())) {
45 return derived().to_hash();
46 }
47};
48
49template <typename T>
50struct hash<T, enable_if_t<is_base_of<ihashable<T>, T>::value>> {
51 MSTL_NODISCARD constexpr size_t operator ()(const T& obj) const
52 noexcept(noexcept(obj.to_hash())) {
53 return obj.to_hash();
54 }
55};
56
57
66template <typename T>
68private:
69 constexpr const T& derived() const noexcept {
70 return static_cast<const T&>(*this);
71 }
72
73public:
81 MSTL_NODISCARD constexpr bool operator ==(const T& rhs) const
82 noexcept(noexcept(derived() == rhs)) {
83 return derived() == rhs;
84 }
85
93 MSTL_NODISCARD constexpr bool operator !=(const T& rhs) const
94 noexcept(noexcept(!(*this == rhs))) {
95 return !(*this == rhs);
96 }
97
105 MSTL_NODISCARD constexpr bool operator <(const T& rhs) const
106 noexcept(noexcept(derived() < rhs)) {
107 return derived() < rhs;
108 }
109
117 MSTL_NODISCARD constexpr bool operator >(const T& rhs) const
118 noexcept(noexcept(rhs < derived())) {
119 return rhs < derived();
120 }
121
129 MSTL_NODISCARD constexpr bool operator <=(const T& rhs) const
130 noexcept(noexcept(!(derived() > rhs))) {
131 return !(derived() > rhs);
132 }
133
141 MSTL_NODISCARD constexpr bool operator >=(const T& rhs) const
142 noexcept(noexcept(!(derived() < rhs))) {
143 return !(derived() < rhs);
144 }
145};
146
147
157template <typename T>
158struct icommon : icomparable<T>, ihashable<T> {};
159 // CRTPInterfaces
161
163#endif // MSTL_CORE_INTERFACE_ICOMMON_HPP__
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL哈希函数库
哈希函数的主模板
通用接口,同时具备可比较和可哈希功能
可比较对象接口模板
MSTL_NODISCARD constexpr bool operator==(const T &rhs) const noexcept(noexcept(derived()==rhs))
相等比较运算符
MSTL_NODISCARD constexpr bool operator!=(const T &rhs) const noexcept(noexcept(!(*this==rhs)))
不等比较运算符
MSTL_NODISCARD constexpr bool operator<=(const T &rhs) const noexcept(noexcept(!(derived() > rhs)))
小于等于比较运算符
MSTL_NODISCARD constexpr bool operator<(const T &rhs) const noexcept(noexcept(derived()< rhs))
小于比较运算符
MSTL_NODISCARD constexpr bool operator>=(const T &rhs) const noexcept(noexcept(!(derived()< rhs)))
大于等于比较运算符
MSTL_NODISCARD constexpr bool operator>(const T &rhs) const noexcept(noexcept(rhs< derived()))
大于比较运算符
可哈希对象接口模板
MSTL_NODISCARD constexpr size_t to_hash() const noexcept(noexcept(derived().to_hash()))
获取对象的哈希值
判断Base是否是Derived的基类