diff --git a/CHANGELOG.md b/CHANGELOG.md index e003c04..17d45a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. ## Next Release +### Types + +- add `std::hash` function to strong type definition + ## [0.2.2](https://github.com/repo/owner/releases/tag/0.2.2) - 2026-08-22 diff --git a/include/mstd/types/strong_type.hpp b/include/mstd/types/strong_type.hpp index f0d031f..e46b596 100644 --- a/include/mstd/types/strong_type.hpp +++ b/include/mstd/types/strong_type.hpp @@ -33,15 +33,17 @@ namespace mstd { +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)sss #define STRONG_TYPE_TRAIT_LIST(X) \ X(NONE, 0) \ - X(ORDERED, 1u << 0) \ - X(ARITHMETIC, 1u << 1) \ - X(INCREMENT, 1u << 2) \ - X(HASHABLE, 1u << 3) \ - X(STREAMABLE, 1u << 4) \ - X(BOOLEAN, 1u << 5) - + X(ORDERED, 1U << 0) \ + X(ARITHMETIC, 1U << 1) \ + X(INCREMENT, 1U << 2) \ + X(HASHABLE, 1U << 3) \ + X(STREAMABLE, 1U << 4) \ + X(BOOLEAN, 1U << 5) + + // NOLINTNEXTLINE(hicpp-signed-bitwise) MSTD_ENUM_BITFLAG(StrongTypeTrait, unsigned, STRONG_TYPE_TRAIT_LIST); /** @@ -115,52 +117,52 @@ namespace mstd // constexpr StrongType &operator+=(const StrongType &other) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); constexpr StrongType &operator-=(const StrongType &other) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); constexpr StrongType &operator*=(const T &scalar) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); constexpr StrongType &operator/=(const T &scalar) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); [[nodiscard]] constexpr StrongType operator+(const StrongType &other) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); [[nodiscard]] constexpr StrongType operator-(const StrongType &other) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); [[nodiscard]] constexpr StrongType operator*(const T &scalar) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); [[nodiscard]] constexpr StrongType operator/(const T &scalar) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); [[nodiscard]] constexpr StrongType operator-() const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)); + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)); // // Increment / decrement // constexpr StrongType &operator++() - requires(bool(Traits &StrongTypeTrait::INCREMENT)); + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)); constexpr StrongType operator++(int) - requires(bool(Traits &StrongTypeTrait::INCREMENT)); + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)); constexpr StrongType &operator--() - requires(bool(Traits &StrongTypeTrait::INCREMENT)); + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)); constexpr StrongType operator--(int) - requires(bool(Traits &StrongTypeTrait::INCREMENT)); + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)); std::string toString() const requires HasToString; @@ -183,4 +185,24 @@ requires(static_cast(Traits &mstd::StrongTypeTrait::STREAMABLE) && true) return os << strongType.get(); } +namespace std +{ + // NOLINTBEGIN(cert-dcl58-cpp) + template + struct hash> + { + [[nodiscard]] size_t operator()( + const mstd::StrongType &value + ) const noexcept(noexcept(std::hash{}(value.get()))) + requires( + static_cast(Traits &mstd::StrongTypeTrait::HASHABLE) && + requires(const T &t) { std::hash{}(t); } + ) + { + return std::hash{}(value.get()); + } + }; + // NOLINTEND(cert-dcl58-cpp) +} // namespace std + #endif // __MSTD__TYPES__STRONG_TYPE_HPP__ diff --git a/include/mstd/types/strong_type.tpp b/include/mstd/types/strong_type.tpp index 8842ac1..3d94345 100644 --- a/include/mstd/types/strong_type.tpp +++ b/include/mstd/types/strong_type.tpp @@ -141,7 +141,7 @@ namespace mstd template constexpr StrongType &StrongType:: operator+=(const StrongType &other) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { _value += other._value; return *this; @@ -166,7 +166,7 @@ namespace mstd template constexpr StrongType &StrongType:: operator-=(const StrongType &other) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { _value -= other._value; return *this; @@ -190,7 +190,7 @@ namespace mstd template constexpr StrongType &StrongType:: operator*=(const T &scalar) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { _value *= scalar; return *this; @@ -214,7 +214,7 @@ namespace mstd template constexpr StrongType &StrongType:: operator/=(const T &scalar) - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { _value /= scalar; return *this; @@ -239,7 +239,7 @@ namespace mstd constexpr StrongType StrongType::operator+( const StrongType &other ) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { return StrongType{_value + other._value}; } @@ -264,7 +264,7 @@ namespace mstd constexpr StrongType StrongType::operator-( const StrongType &other ) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { return StrongType{_value - other._value}; } @@ -288,7 +288,7 @@ namespace mstd constexpr StrongType StrongType::operator*( const T &scalar ) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { return StrongType{_value * scalar}; } @@ -312,7 +312,7 @@ namespace mstd constexpr StrongType StrongType::operator/( const T &scalar ) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { return StrongType{_value / scalar}; } @@ -334,7 +334,7 @@ namespace mstd template constexpr StrongType StrongType::operator-( ) const - requires(bool(Traits &StrongTypeTrait::ARITHMETIC)) + requires(static_cast(Traits &StrongTypeTrait::ARITHMETIC)) { return StrongType{-_value}; } @@ -356,7 +356,7 @@ namespace mstd template constexpr StrongType &StrongType:: operator++() - requires(bool(Traits &StrongTypeTrait::INCREMENT)) + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)) { ++_value; return *this; @@ -380,7 +380,7 @@ namespace mstd constexpr StrongType StrongType::operator++( int ) - requires(bool(Traits &StrongTypeTrait::INCREMENT)) + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)) { auto tmp = *this; ++_value; @@ -404,7 +404,7 @@ namespace mstd template constexpr StrongType &StrongType:: operator--() - requires(bool(Traits &StrongTypeTrait::INCREMENT)) + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)) { --_value; return *this; @@ -428,7 +428,7 @@ namespace mstd constexpr StrongType StrongType::operator--( int ) - requires(bool(Traits &StrongTypeTrait::INCREMENT)) + requires(static_cast(Traits &StrongTypeTrait::INCREMENT)) { auto tmp = *this; --_value;