Created
February 8, 2018 05:26
-
-
Save LYP951018/334e3309388cf9fc3d1f68b99c3de668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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