64,642
社区成员
发帖
与我相关
我的任务
分享
template<size_t _bits>
class uintx_t {
static_assert(_bits > 64 && (_bits & (_bits - 1)) == 0, "x must be larger than 64 and must be a power of 2.");
public:
static constexpr size_t unit_bits = sizeof(uintptr_t) * 8;
uintptr_t value[_bits / unit_bits];
uintx_t(uintptr_t _value) : value{ _value } { }
template<size_t _others_bits>
uintx_t<_bits>& operator=(const uintx_t<_others_bits>& _other) {
static_assert(_bits >= _others_bits, "Warning: Truncation detected."); // 这里我想要static_warning,
// 因为这里发生了截断,应当给个警告,而不是错误
if (_bits > _others_bits) {
memcpy_s(value, sizeof(value), _other.value, sizeof(_other.value));
memset(value + _others_bits / unit_bits, 0, sizeof(value) - sizeof(_other.value));
} else {
memcpy_s(value, sizeof(value), _other.value, sizeof(value));
}
return *this;
}
};