From fcd55ee92423c54fa0fefcfc0f9fbe9b5a274f56 Mon Sep 17 00:00:00 2001 From: Werner Henze <34543625+beinhaerter@users.noreply.github.com> Date: Mon, 23 Dec 2024 17:40:22 +0100 Subject: [PATCH] Better use of std::enable_if (#1177) * this commits adds tests that should fail, but don't * Better use of std::enable_if Replace the occurances of `class = std::enable_if_t` and `typename = std::enable_if_t` that have been identified in the previous commit with `std::enable_if_t = true`. This commit is inspired by #1174, which changed one occurance in the owner header. This commit is aimed to fix all remaining occurances. * fix failing checks - core.cxx_gsl aktualisiert auf [](https://gitlab.avm.de/fos/repos/core.cxx_gsl/-/commit/) - plc.access_lib aktualisiert auf [](https://gitlab.avm.de/fos/repos/plc.access_lib/-/commit/) - plc.common aktualisiert auf [](https://gitlab.avm.de/fos/repos/plc.common/-/commit/) - plc.daemon aktualisiert auf [](https://gitlab.avm.de/fos/repos/plc.daemon/-/commit/) - Test Plan: - --------- Co-authored-by: Werner Henze Co-authored-by: Werner Henze --- docs/headers.md | 12 ++++---- include/gsl/byte | 10 +++---- include/gsl/pointers | 2 +- tests/byte_tests.cpp | 45 ++++++++++++++++++++++++++++++ tests/pointers_tests.cpp | 41 +++++++++++++++++++++++++++ tests/span_compatibility_tests.cpp | 9 ++++-- 6 files changed, 105 insertions(+), 14 deletions(-) diff --git a/docs/headers.md b/docs/headers.md index 5f4eef1..033cd8a 100644 --- a/docs/headers.md +++ b/docs/headers.md @@ -91,16 +91,16 @@ See [SL.str.5: Use `std::byte` to refer to byte values that do not necessarily r ### Non-member functions ```cpp -template ::value>> +template ::value, bool> = true> constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept; -template ::value>> +template ::value, bool> = true> constexpr byte operator<<(byte b, IntegerType shift) noexcept; -template ::value>> +template ::value, bool> = true> constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept; -template ::value>> +template ::value, bool> = true> constexpr byte operator>>(byte b, IntegerType shift) noexcept; ``` @@ -134,7 +134,7 @@ constexpr byte operator~(byte b) noexcept; Bitwise negation of a `byte`. Flips all bits. Zeroes become ones, ones become zeroes. ```cpp -template ::value>> +template ::value, bool> = true> constexpr IntegerType to_integer(byte b) noexcept; ``` @@ -310,7 +310,7 @@ auto make_not_null(T&& t) noexcept; Creates a `gsl::not_null` object, deducing the target type from the type of the argument. ```cpp -template ::value && std::is_move_constructible::value>> +template ::value && std::is_move_constructible::value, bool> = true> void swap(not_null& a, not_null& b); ``` diff --git a/include/gsl/byte b/include/gsl/byte index 87d007e..72044bb 100644 --- a/include/gsl/byte +++ b/include/gsl/byte @@ -91,25 +91,25 @@ enum class byte_may_alias byte : unsigned char { }; -template ::value>> +template ::value, bool> = true> constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept { return b = byte(static_cast(b) << shift); } -template ::value>> +template ::value, bool> = true> constexpr byte operator<<(byte b, IntegerType shift) noexcept { return byte(static_cast(b) << shift); } -template ::value>> +template ::value, bool> = true> constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept { return b = byte(static_cast(b) >> shift); } -template ::value>> +template ::value, bool> = true> constexpr byte operator>>(byte b, IntegerType shift) noexcept { return byte(static_cast(b) >> shift); @@ -147,7 +147,7 @@ constexpr byte operator^(byte l, byte r) noexcept constexpr byte operator~(byte b) noexcept { return byte(~static_cast(b)); } -template ::value>> +template ::value, bool> = true> constexpr IntegerType to_integer(byte b) noexcept { return static_cast(b); diff --git a/include/gsl/pointers b/include/gsl/pointers index cd07939..78d191c 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -145,7 +145,7 @@ private: T ptr_; }; -template ::value && std::is_move_constructible::value>> +template ::value && std::is_move_constructible::value, bool> = true> void swap(not_null& a, not_null& b) { a.swap(b); diff --git a/tests/byte_tests.cpp b/tests/byte_tests.cpp index 1d2052a..ca94a9b 100644 --- a/tests/byte_tests.cpp +++ b/tests/byte_tests.cpp @@ -19,6 +19,9 @@ #define GSL_USE_STD_BYTE 0 #include // for to_byte, to_integer, byte, operator&, ope... +#include +#include + using namespace std; using namespace gsl; @@ -128,6 +131,48 @@ TEST(byte_tests, aliasing) EXPECT_TRUE(res == i); } +#if __cplusplus >= 201703l +using std::void_t; +#else // __cplusplus >= 201703l +template +using void_t = void; +#endif // __cplusplus < 201703l + +template +static constexpr bool LShiftCompilesFor = false; +template +static constexpr bool LShiftCompilesFor< + U, void_t(declval(), declval()))>> = true; +static_assert(!LShiftCompilesFor, "!LShiftCompilesFor"); + +template +static constexpr bool RShiftCompilesFor = false; +template +static constexpr bool RShiftCompilesFor< + U, void_t> (declval(), declval()))>> = true; +static_assert(!RShiftCompilesFor, "!RShiftCompilesFor"); + +template +static constexpr bool LShiftAssignCompilesFor = false; +template +static constexpr bool LShiftAssignCompilesFor< + U, void_t(declval(), declval()))>> = true; +static_assert(!LShiftAssignCompilesFor, "!LShiftAssignCompilesFor"); + +template +static constexpr bool RShiftAssignCompilesFor = false; +template +static constexpr bool RShiftAssignCompilesFor< + U, void_t>= (declval(), declval()))>> = true; +static_assert(!RShiftAssignCompilesFor, "!RShiftAssignCompilesFor"); + +template +static constexpr bool ToIntegerCompilesFor = false; +template +static constexpr bool + ToIntegerCompilesFor(gsl::byte{}))>> = true; +static_assert(!ToIntegerCompilesFor, "!ToIntegerCompilesFor"); + } // namespace #ifdef CONFIRM_COMPILATION_ERRORS diff --git a/tests/pointers_tests.cpp b/tests/pointers_tests.cpp index fc9fb45..673e184 100644 --- a/tests/pointers_tests.cpp +++ b/tests/pointers_tests.cpp @@ -3,6 +3,24 @@ #include #include +#include +#include + +namespace +{ +// Custom pointer type that can be used for gsl::not_null, but for which these cannot be swapped. +struct NotMoveAssignableCustomPtr +{ + NotMoveAssignableCustomPtr() = default; + NotMoveAssignableCustomPtr(const NotMoveAssignableCustomPtr&) = default; + NotMoveAssignableCustomPtr& operator=(const NotMoveAssignableCustomPtr&) = default; + NotMoveAssignableCustomPtr(NotMoveAssignableCustomPtr&&) = default; + NotMoveAssignableCustomPtr& operator=(NotMoveAssignableCustomPtr&&) = delete; + + bool operator!=(std::nullptr_t) const { return true; } + + int dummy{}; // Without this clang warns, that NotMoveAssignableCustomPtr() is unneeded +}; TEST(pointers_test, swap) { @@ -17,5 +35,28 @@ TEST(pointers_test, swap) EXPECT_TRUE(*a == 1); EXPECT_TRUE(*b == 0); + + // Make sure our custom ptr can be used with not_null. The shared_pr is to prevent "unused" + // compiler warnings. + const auto shared_custom_ptr{std::make_shared()}; + gsl::not_null c{*shared_custom_ptr}; + EXPECT_TRUE(c.get() != nullptr); } +#if __cplusplus >= 201703l +using std::void_t; +#else // __cplusplus >= 201703l +template +using void_t = void; +#endif // __cplusplus < 201703l + +template +static constexpr bool SwapCompilesFor = false; +template +static constexpr bool + SwapCompilesFor(std::declval&>(), + std::declval&>()))>> = true; +static_assert(!SwapCompilesFor, + "!SwapCompilesFor"); + +} // namespace diff --git a/tests/span_compatibility_tests.cpp b/tests/span_compatibility_tests.cpp index c6e4b02..3aad961 100644 --- a/tests/span_compatibility_tests.cpp +++ b/tests/span_compatibility_tests.cpp @@ -1005,12 +1005,18 @@ static_assert(std::is_convertible&, gsl::span&, gsl::span>"); #if __cplusplus >= 201703l +using std::void_t; +#else // __cplusplus >= 201703l +template +using void_t = void; +#endif // __cplusplus < 201703l + template static constexpr bool AsWritableBytesCompilesFor = false; template static constexpr bool - AsWritableBytesCompilesFor()))>> = true; + AsWritableBytesCompilesFor()))>> = true; static_assert(AsWritableBytesCompilesFor>, "AsWritableBytesCompilesFor>"); @@ -1020,4 +1026,3 @@ static_assert(!AsWritableBytesCompilesFor>, "!AsWritableBytesCompilesFor>"); static_assert(!AsWritableBytesCompilesFor>, "!AsWritableBytesCompilesFor>"); -#endif // __cplusplus >= 201703l