mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
simplify to_byte (#1090)
- to_byte_impl is not necessary, the same can be achieved with shorter code - add test code for things that should not compile
This commit is contained in:
parent
49c88f27bd
commit
3ba80d5dd4
@ -174,26 +174,15 @@ constexpr IntegerType to_integer(byte b) noexcept
|
|||||||
|
|
||||||
#endif // GSL_USE_STD_BYTE
|
#endif // GSL_USE_STD_BYTE
|
||||||
|
|
||||||
template <bool E, typename T>
|
template <typename T>
|
||||||
constexpr byte to_byte_impl(T t) noexcept
|
|
||||||
{
|
|
||||||
static_assert(
|
|
||||||
E, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
|
|
||||||
"If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
|
|
||||||
return static_cast<byte>(t);
|
|
||||||
}
|
|
||||||
template <>
|
|
||||||
// NOTE: need suppression since c++14 does not allow "return {t}"
|
// NOTE: need suppression since c++14 does not allow "return {t}"
|
||||||
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
|
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
|
||||||
constexpr byte to_byte_impl<true, unsigned char>(unsigned char t) noexcept
|
|
||||||
{
|
|
||||||
return byte(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr byte to_byte(T t) noexcept
|
constexpr byte to_byte(T t) noexcept
|
||||||
{
|
{
|
||||||
return to_byte_impl<std::is_same<T, unsigned char>::value, T>(t);
|
static_assert(std::is_same<T, unsigned char>::value,
|
||||||
|
"gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
|
||||||
|
"If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
|
||||||
|
return byte(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <int I>
|
template <int I>
|
||||||
|
@ -61,6 +61,12 @@ TEST(byte_tests, construction)
|
|||||||
EXPECT_TRUE(static_cast<unsigned char>(b) == 14);
|
EXPECT_TRUE(static_cast<unsigned char>(b) == 14);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||||
|
to_byte(char{});
|
||||||
|
to_byte(3);
|
||||||
|
to_byte(3u);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(byte_tests, bitwise_operations)
|
TEST(byte_tests, bitwise_operations)
|
||||||
|
Loading…
Reference in New Issue
Block a user