From b1757956549cbf61bc26a0b91eabce1e4216939d Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Mon, 15 Jun 2020 17:09:44 -0700 Subject: [PATCH 1/2] Cleanup ranges test machinery Use helper function `to_bool` to replace function-style casts to `bool`. It's cleaner while still being more concise than a full `static_cast`. --- tests/std/include/range_algorithm_support.hpp | 56 ++++++++++--------- .../tests/P0896R4_ranges_subrange/test.cpp | 6 +- .../P0896R4_ranges_test_machinery/test.cpp | 20 +++---- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/tests/std/include/range_algorithm_support.hpp b/tests/std/include/range_algorithm_support.hpp index 6a1ecd931ca..54784b6657f 100644 --- a/tests/std/include/range_algorithm_support.hpp +++ b/tests/std/include/range_algorithm_support.hpp @@ -87,6 +87,12 @@ namespace test { enum class ProxyRef : bool { no, yes }; enum class IsWrapped : bool { no, yes }; + template + [[nodiscard]] constexpr bool to_bool(T const t) noexcept { + STATIC_ASSERT(std::is_enum_v && std::same_as, bool>); + return static_cast(t); + } + template class sentinel { Element* ptr_ = nullptr; @@ -103,13 +109,13 @@ namespace test { using unwrap = sentinel; - [[nodiscard]] constexpr auto _Unwrapped() const noexcept requires(bool(Wrapped)) { + [[nodiscard]] constexpr auto _Unwrapped() const noexcept requires(to_bool(Wrapped)) { return unwrap{ptr_}; } static constexpr bool _Unwrap_when_unverified = true; - constexpr void _Seek_to(unwrap const& s) noexcept requires(bool(Wrapped)) { + constexpr void _Seek_to(unwrap const& s) noexcept requires(to_bool(Wrapped)) { ptr_ = s.base(); } }; @@ -168,15 +174,15 @@ namespace test { ProxyRef Proxy = ProxyRef{!derived_from}, // Interact with the STL's iterator unwrapping machinery? IsWrapped Wrapped = IsWrapped::yes> - requires (bool(Eq) || !derived_from) - && (!bool(Proxy) || !derived_from) + requires (to_bool(Eq) || !derived_from) + && (!to_bool(Proxy) || !derived_from) class iterator { Element* ptr_; template static constexpr bool at_least = derived_from; - using ReferenceType = conditional_t, Element&>; + using ReferenceType = conditional_t, Element&>; public: // output iterator operations @@ -190,7 +196,7 @@ namespace test { return *this; } - [[nodiscard]] constexpr Element* base() const& noexcept requires (bool(Eq)) { + [[nodiscard]] constexpr Element* base() const& noexcept requires (to_bool(Eq)) { return ptr_; } [[nodiscard]] constexpr Element* base() && noexcept { @@ -264,12 +270,12 @@ namespace test { } // sentinel operations (implied by forward iterator): - iterator(iterator const&) requires (bool(Eq)) = default; - iterator& operator=(iterator const&) requires (bool(Eq)) = default; - [[nodiscard]] constexpr boolish operator==(iterator const& that) const noexcept requires (bool(Eq)) { + iterator(iterator const&) requires (to_bool(Eq)) = default; + iterator& operator=(iterator const&) requires (to_bool(Eq)) = default; + [[nodiscard]] constexpr boolish operator==(iterator const& that) const noexcept requires (to_bool(Eq)) { return {ptr_ == that.ptr_}; } - [[nodiscard]] constexpr boolish operator!=(iterator const& that) const noexcept requires (bool(Eq)) { + [[nodiscard]] constexpr boolish operator!=(iterator const& that) const noexcept requires (to_bool(Eq)) { return !(*this == that); } @@ -326,15 +332,15 @@ namespace test { // sized_sentinel_for operations: [[nodiscard]] constexpr ptrdiff_t operator-(iterator const& that) const noexcept - requires (bool(Diff) && bool(Eq)) || at_least { + requires (to_bool(Diff) && to_bool(Eq)) || at_least { return ptr_ - that.ptr_; } [[nodiscard]] constexpr ptrdiff_t operator-(sentinel const& s) const noexcept - requires (bool(Diff)) { + requires (to_bool(Diff)) { return ptr_ - s.base(); } [[nodiscard]] friend constexpr ptrdiff_t operator-( - sentinel const& s, iterator const& i) noexcept requires (bool(Diff)) { + sentinel const& s, iterator const& i) noexcept requires (to_bool(Diff)) { return -(i - s); } @@ -343,21 +349,21 @@ namespace test { using unwrap = iterator; - [[nodiscard]] constexpr auto _Unwrapped() const& noexcept requires (bool(Wrapped) && bool(Eq)) { + [[nodiscard]] constexpr auto _Unwrapped() const& noexcept requires (to_bool(Wrapped) && to_bool(Eq)) { return unwrap{ptr_}; } - [[nodiscard]] constexpr auto _Unwrapped() && noexcept requires (bool(Wrapped)) { + [[nodiscard]] constexpr auto _Unwrapped() && noexcept requires (to_bool(Wrapped)) { return unwrap{exchange(ptr_, nullptr)}; } static constexpr bool _Unwrap_when_unverified = true; - constexpr void _Seek_to(unwrap const& i) noexcept requires (bool(Wrapped) && bool(Eq)) { + constexpr void _Seek_to(unwrap const& i) noexcept requires (to_bool(Wrapped) && to_bool(Eq)) { ptr_ = i.base(); } - constexpr void _Seek_to(unwrap&& i) noexcept requires (bool(Wrapped)) { + constexpr void _Seek_to(unwrap&& i) noexcept requires (to_bool(Wrapped)) { ptr_ = std::move(i).base(); } }; @@ -369,8 +375,8 @@ template > { using iterator_concept = Category; using iterator_category = conditional_t, // - conditional_t, // - conditional_t>; // TRANSITION, LWG-3289 + conditional_t(Proxy), input_iterator_tag, Category>, // + conditional_t(Eq), Category, void>>; // TRANSITION, LWG-3289 using value_type = remove_cv_t; using difference_type = ptrdiff_t; using pointer = conditional_t, Element*, void>; @@ -406,16 +412,16 @@ namespace test { CanCompare Eq = CanCompare{derived_from}, // Use a ProxyRef reference type? ProxyRef Proxy = ProxyRef{!derived_from}> - requires (!bool(IsCommon) || bool(Eq)) - && (bool(Eq) || !derived_from) - && (!bool(Proxy) || !derived_from) + requires (!to_bool(IsCommon) || to_bool(Eq)) + && (to_bool(Eq) || !derived_from) + && (!to_bool(Proxy) || !derived_from) class range : ranges::view_base { span elements_; mutable bool begin_called_ = false; public: using I = iterator; - using S = conditional_t>; + using S = conditional_t>; range() = default; constexpr explicit range(span elements) noexcept : elements_{elements} {} @@ -443,7 +449,7 @@ namespace test { return S{elements_.data() + elements_.size()}; } - [[nodiscard]] constexpr ptrdiff_t size() const noexcept requires (bool(IsSized)) { + [[nodiscard]] constexpr ptrdiff_t size() const noexcept requires (to_bool(IsSized)) { if constexpr (!derived_from) { assert(!begin_called_); } @@ -455,7 +461,7 @@ namespace test { } using UI = iterator; - using US = conditional_t>; + using US = conditional_t>; [[nodiscard]] constexpr UI _Unchecked_begin() const noexcept { return UI{elements_.data()}; diff --git a/tests/std/tests/P0896R4_ranges_subrange/test.cpp b/tests/std/tests/P0896R4_ranges_subrange/test.cpp index 74ab977ce65..65048e32232 100644 --- a/tests/std/tests/P0896R4_ranges_subrange/test.cpp +++ b/tests/std/tests/P0896R4_ranges_subrange/test.cpp @@ -98,13 +98,13 @@ namespace test_view_interface { template struct fake_view : ranges::view_interface> { using I = test::iterator; - using S = std::conditional_t>; + using S = std::conditional_t(IsCommon), I, test::sentinel>; I begin(); - I begin() const requires (bool(HasConstRange)); + I begin() const requires (static_cast(HasConstRange)); S end(); - S end() const requires (bool(HasConstRange)); + S end() const requires (static_cast(HasConstRange)); }; // clang-format on diff --git a/tests/std/tests/P0896R4_ranges_test_machinery/test.cpp b/tests/std/tests/P0896R4_ranges_test_machinery/test.cpp index 33a1897e981..9f362e7e5ff 100644 --- a/tests/std/tests/P0896R4_ranges_test_machinery/test.cpp +++ b/tests/std/tests/P0896R4_ranges_test_machinery/test.cpp @@ -12,7 +12,7 @@ using namespace std; int main() {} // COMPILE-ONLY -using test::CanDifference, test::CanCompare, test::ProxyRef, test::IsWrapped; +using test::CanCompare, test::CanDifference, test::IsWrapped, test::ProxyRef, test::to_bool; // Validate test::iterator and test::sentinel template @@ -39,11 +39,11 @@ constexpr bool iter_test() { using S = sentinel; STATIC_ASSERT(sentinel_for); - STATIC_ASSERT(!bool(Diff) || sized_sentinel_for); - STATIC_ASSERT(!bool(Eq) || sentinel_for); - STATIC_ASSERT(!bool(Eq) || !bool(Diff) || sized_sentinel_for); + STATIC_ASSERT(!to_bool(Diff) || sized_sentinel_for); + STATIC_ASSERT(!to_bool(Eq) || sentinel_for); + STATIC_ASSERT(!to_bool(Eq) || !to_bool(Diff) || sized_sentinel_for); - if constexpr (bool(Wrapped)) { + if constexpr (to_bool(Wrapped)) { STATIC_ASSERT(same_as<_Unwrapped_t, iterator>); STATIC_ASSERT(same_as<_Unwrapped_t, sentinel>); } @@ -170,18 +170,18 @@ constexpr bool range_test() { STATIC_ASSERT(!derived_from || ranges::random_access_range); STATIC_ASSERT(!derived_from || ranges::contiguous_range); - if constexpr (bool(IsCommon)) { - STATIC_ASSERT(bool(Eq)); + if constexpr (to_bool(IsCommon)) { + STATIC_ASSERT(to_bool(Eq)); STATIC_ASSERT(ranges::common_range); } else { STATIC_ASSERT(same_as>); } - STATIC_ASSERT(!bool(Eq) || sentinel_for); + STATIC_ASSERT(!to_bool(Eq) || sentinel_for); - constexpr bool is_sized = bool(IsSized) || (bool(Diff) && derived_from); + constexpr bool is_sized = to_bool(IsSized) || (to_bool(Diff) && derived_from); STATIC_ASSERT(ranges::sized_range == is_sized); - if constexpr (bool(IsSized)) { + if constexpr (to_bool(IsSized)) { STATIC_ASSERT(has_member_size); } From f58e0c5d0c53a88479e69c619b7de1acfb9b6943 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Mon, 15 Jun 2020 17:25:25 -0700 Subject: [PATCH 2/2] Review comment --- tests/std/tests/P0896R4_ranges_subrange/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/std/tests/P0896R4_ranges_subrange/test.cpp b/tests/std/tests/P0896R4_ranges_subrange/test.cpp index 65048e32232..1716ef933a7 100644 --- a/tests/std/tests/P0896R4_ranges_subrange/test.cpp +++ b/tests/std/tests/P0896R4_ranges_subrange/test.cpp @@ -91,20 +91,20 @@ namespace test_view_interface { t[42]; }; - using test::Common, test::CanDifference, test::CanCompare, test::ProxyRef; + using test::CanCompare, test::CanDifference, test::Common, test::ProxyRef, test::to_bool; enum class ConstRange : bool { no, yes }; // clang-format off template struct fake_view : ranges::view_interface> { using I = test::iterator; - using S = std::conditional_t(IsCommon), I, test::sentinel>; + using S = std::conditional_t>; I begin(); - I begin() const requires (static_cast(HasConstRange)); + I begin() const requires (to_bool(HasConstRange)); S end(); - S end() const requires (static_cast(HasConstRange)); + S end() const requires (to_bool(HasConstRange)); }; // clang-format on