Skip to content

Instantly share code, notes, and snippets.

@LYP951018
Created February 8, 2018 05:26
Show Gist options
  • Save LYP951018/334e3309388cf9fc3d1f68b99c3de668 to your computer and use it in GitHub Desktop.
Save LYP951018/334e3309388cf9fc3d1f68b99c3de668 to your computer and use it in GitHub Desktop.
#include <type_traits>
#include <limits>
#include <cassert>
enum class uint32_t : unsigned int {};
constexpr uint32_t operator""_u32(unsigned long long x) {
using underlying = std::underlying_type_t<uint32_t>;
assert(static_cast<unsigned long long>(std::numeric_limits<underlying>::max()) >= x);
return uint32_t{static_cast<underlying>(x)};
}
template<typename T, typename... Args>
constexpr bool is_one_of = std::disjunction_v<std::is_same<T, Args>...>;
template<typename T>
concept bool Integral = is_one_of<T, uint32_t>;
template<Integral T>
constexpr T operator+ (T lhs, T rhs) noexcept
{
using underlying = std::underlying_type_t<uint32_t>;
return T{static_cast<underlying>(lhs) + static_cast<underlying>(rhs)};
}
int main() {
auto x = 1_u32;
auto y = x + 2_u32;
assert(x + y == 4_u32);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment