File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,12 @@ constexpr bool is_nonzero(Type value) noexcept
4444 return !is_zero (value);
4545}
4646
47+ template <typename Type>
48+ constexpr Type zeroize (Type& value) noexcept
49+ {
50+ return value ^= value;
51+ }
52+
4753template <typename Type>
4854constexpr bool is_one (Type value) noexcept
4955{
Original file line number Diff line number Diff line change @@ -30,6 +30,24 @@ static_assert(is_nonzero(1u));
3030static_assert (is_nonzero(0xff ));
3131static_assert (is_same_type<decltype (is_nonzero<int16_t >(0 )), bool >);
3232
33+ const auto zeroize_helper = [](auto value)
34+ {
35+ using type = decltype (value);
36+ return zeroize (value);
37+ static_assert (is_same_type<decltype (zeroize<type>(value)), type>);
38+ };
39+ static_assert (zeroize_helper(0_u8) == 0u );
40+ static_assert (zeroize_helper(1_u8) == 0u );
41+ static_assert (zeroize_helper(3_u16) == 0u );
42+ static_assert (zeroize_helper(4_u16) == 0u );
43+ static_assert (zeroize_helper(max_uint32) == 0u );
44+ static_assert (zeroize_helper(0_i8) == 0 );
45+ static_assert (zeroize_helper(1_i8) == 0 );
46+ static_assert (zeroize_helper(3_i16) == 0 );
47+ static_assert (zeroize_helper(4_i16) == 0 );
48+ static_assert (zeroize_helper(max_int32) == 0 );
49+ static_assert (zeroize_helper(min_int32) == 0 );
50+
3351static_assert (is_one(1 ));
3452static_assert (!is_one(0u ));
3553static_assert (!is_one(0xff ));
You can’t perform that action at this time.
0 commit comments