// constexpr V base() &&;
#include <ranges>
+
#include <cassert>
+#include <utility>
#include "test_macros.h"
-#include "test_iterators.h"
-#include "test_range.h"
-
-struct ContiguousView : std::ranges::view_base {
- int *ptr_;
- constexpr ContiguousView(int* ptr) : ptr_(ptr) {}
- constexpr ContiguousView(ContiguousView&&) = default;
- constexpr ContiguousView& operator=(ContiguousView&&) = default;
- friend constexpr int* begin(ContiguousView& view) { return view.ptr_; }
- friend constexpr int* begin(ContiguousView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
-
-struct CopyableView : std::ranges::view_base {
- int *ptr_;
- constexpr CopyableView(int* ptr) : ptr_(ptr) {}
- friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
- friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
+#include "types.h"
constexpr bool hasLValueQualifiedBase(auto&& view) {
- return requires { view.base(); };
+ return requires { view.base(); };
}
constexpr bool test() {
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
{
- std::ranges::common_view<CopyableView> common(CopyableView{buffer});
- assert(common.base().ptr_ == buffer);
- assert(std::move(common).base().ptr_ == buffer);
+ CopyableView view{buf, buf + 8};
+ std::ranges::common_view<CopyableView> common(view);
+ assert(common.base().begin_ == buf);
+ assert(std::move(common).base().begin_ == buf);
ASSERT_SAME_TYPE(decltype(common.base()), CopyableView);
ASSERT_SAME_TYPE(decltype(std::move(common).base()), CopyableView);
}
{
- std::ranges::common_view<ContiguousView> common(ContiguousView{buffer});
- assert(std::move(common).base().ptr_ == buffer);
+ ContiguousView view{buf, buf + 8};
+ std::ranges::common_view<ContiguousView> common(std::move(view));
+ assert(std::move(common).base().begin_ == buf);
ASSERT_SAME_TYPE(decltype(std::move(common).base()), ContiguousView);
static_assert(!hasLValueQualifiedBase(common));
}
{
- const std::ranges::common_view<CopyableView> common(CopyableView{buffer});
- assert(common.base().ptr_ == buffer);
- assert(std::move(common).base().ptr_ == buffer);
+ CopyableView view{buf, buf + 8};
+ const std::ranges::common_view<CopyableView> common(view);
+ assert(common.base().begin_ == buf);
+ assert(std::move(common).base().begin_ == buf);
ASSERT_SAME_TYPE(decltype(common.base()), CopyableView);
ASSERT_SAME_TYPE(decltype(std::move(common).base()), CopyableView);
// constexpr auto begin() const requires range<const V>;
#include <ranges>
+
#include <cassert>
+#include <concepts>
+#include <utility>
-#include "test_macros.h"
#include "test_iterators.h"
-#include "test_range.h"
-
-struct ContiguousView : std::ranges::view_base {
- int *ptr_;
- constexpr ContiguousView(int* ptr) : ptr_(ptr) {}
- constexpr ContiguousView(ContiguousView&&) = default;
- constexpr ContiguousView& operator=(ContiguousView&&) = default;
- friend constexpr int* begin(ContiguousView& view) { return view.ptr_; }
- friend constexpr int* begin(ContiguousView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
-
-struct CopyableView : std::ranges::view_base {
- int *ptr_;
- constexpr CopyableView(int* ptr) : ptr_(ptr) {}
- friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
- friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
+#include "types.h"
struct MutableView : std::ranges::view_base {
- int *ptr_;
- constexpr MutableView(int* ptr) : ptr_(ptr) {}
- constexpr int* begin() { return ptr_; }
- constexpr sentinel_wrapper<int*> end() { return sentinel_wrapper<int*>{ptr_ + 8}; }
-};
-
-using ForwardIter = forward_iterator<int*>;
-struct SizedForwardView : std::ranges::view_base {
- int *ptr_;
- constexpr SizedForwardView(int* ptr) : ptr_(ptr) {}
- friend constexpr ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.ptr_); }
- friend constexpr ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.ptr_); }
- friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView& view) {
- return sentinel_wrapper<ForwardIter>{ForwardIter(view.ptr_ + 8)};
- }
- friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView const& view) {
- return sentinel_wrapper<ForwardIter>{ForwardIter(view.ptr_ + 8)};
- }
+ int* begin();
+ sentinel_wrapper<int*> end();
};
-// Required to make SizedForwardView a sized view.
-constexpr auto operator-(sentinel_wrapper<ForwardIter> sent, ForwardIter iter) {
- return sent.base().base() - iter.base();
-}
-constexpr auto operator-(ForwardIter iter, sentinel_wrapper<ForwardIter> sent) {
- return iter.base() - sent.base().base();
-}
-using RandomAccessIter = random_access_iterator<int*>;
-struct SizedRandomAccessView : std::ranges::view_base {
- int *ptr_;
- constexpr SizedRandomAccessView(int* ptr) : ptr_(ptr) {}
- friend constexpr RandomAccessIter begin(SizedRandomAccessView& view) { return RandomAccessIter(view.ptr_); }
- friend constexpr RandomAccessIter begin(SizedRandomAccessView const& view) { return RandomAccessIter(view.ptr_); }
- friend constexpr sentinel_wrapper<RandomAccessIter> end(SizedRandomAccessView& view) {
- return sentinel_wrapper<RandomAccessIter>{RandomAccessIter(view.ptr_ + 8)};
- }
- friend constexpr sentinel_wrapper<RandomAccessIter> end(SizedRandomAccessView const& view) {
- return sentinel_wrapper<RandomAccessIter>{RandomAccessIter(view.ptr_ + 8)};
- }
-};
-// Required to make SizedRandomAccessView a sized view.
-constexpr auto operator-(sentinel_wrapper<RandomAccessIter> sent, RandomAccessIter iter) {
- return sent.base().base() - iter.base();
-}
-constexpr auto operator-(RandomAccessIter iter, sentinel_wrapper<RandomAccessIter> sent) {
- return iter.base() - sent.base().base();
-}
-
-template<class T>
-concept BeginEnabled = requires(const std::ranges::common_view<T>& comm) {
- comm.begin();
-};
+template<class View>
+concept BeginEnabled = requires(View v) { v.begin(); };
constexpr bool test() {
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
{
- static_assert( BeginEnabled<CopyableView>);
- static_assert(!BeginEnabled<MutableView>);
+ static_assert( BeginEnabled<std::ranges::common_view<CopyableView> const&>);
+ static_assert( BeginEnabled<std::ranges::common_view<MutableView>&>);
+ static_assert(!BeginEnabled<std::ranges::common_view<MutableView> const&>);
}
{
- std::ranges::common_view<SizedRandomAccessView> comm(SizedRandomAccessView{buffer});
- assert(comm.begin() == begin(SizedRandomAccessView(buffer)));
- ASSERT_SAME_TYPE(decltype(comm.begin()), RandomAccessIter);
+ SizedRandomAccessView view{buf, buf + 8};
+ std::ranges::common_view<SizedRandomAccessView> common(view);
+ std::same_as<RandomAccessIter> auto begin = common.begin();
+ assert(begin == std::ranges::begin(view));
}
{
- const std::ranges::common_view<SizedRandomAccessView> comm(SizedRandomAccessView{buffer});
- assert(comm.begin() == begin(SizedRandomAccessView(buffer)));
- ASSERT_SAME_TYPE(decltype(comm.begin()), RandomAccessIter);
+ SizedRandomAccessView view{buf, buf + 8};
+ std::ranges::common_view<SizedRandomAccessView> const common(view);
+ std::same_as<RandomAccessIter> auto begin = common.begin();
+ assert(begin == std::ranges::begin(view));
}
return true;
static_assert(test());
// The non-constexpr tests:
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
{
- std::ranges::common_view<SizedForwardView> comm(SizedForwardView{buffer});
- assert(comm.begin() == begin(SizedForwardView(buffer)));
- ASSERT_SAME_TYPE(decltype(comm.begin()), std::common_iterator<ForwardIter, sentinel_wrapper<ForwardIter>>);
+ SizedForwardView view{buf, buf + 8};
+ std::ranges::common_view<SizedForwardView> common(view);
+ using CommonIter = std::common_iterator<ForwardIter, sentinel_wrapper<ForwardIter>>;
+ std::same_as<CommonIter> auto begin = common.begin();
+ assert(begin == std::ranges::begin(view));
}
-
{
- std::ranges::common_view<ContiguousView> comm(ContiguousView{buffer});
- assert(comm.begin() == begin(ContiguousView(buffer)));
- ASSERT_SAME_TYPE(decltype(comm.begin()), std::common_iterator<int*, sentinel_wrapper<int*>>);
+ SizedForwardView view{buf, buf + 8};
+ std::ranges::common_view<SizedForwardView> const common(view);
+ using CommonIter = std::common_iterator<ForwardIter, sentinel_wrapper<ForwardIter>>;
+ std::same_as<CommonIter> auto begin = common.begin();
+ assert(begin == std::ranges::begin(view));
}
{
- const std::ranges::common_view<SizedForwardView> comm(SizedForwardView{buffer});
- assert(comm.begin() == begin(SizedForwardView(buffer)));
- ASSERT_SAME_TYPE(decltype(comm.begin()), std::common_iterator<ForwardIter, sentinel_wrapper<ForwardIter>>);
+ ContiguousView view{buf, buf + 8};
+ std::ranges::common_view<ContiguousView> common(std::move(view));
+ using CommonIter = std::common_iterator<int*, sentinel_wrapper<int*>>;
+ std::same_as<CommonIter> auto begin = common.begin();
+ assert(begin == std::ranges::begin(view));
}
{
- const std::ranges::common_view<CopyableView> comm(CopyableView{buffer});
- assert(comm.begin() == begin(CopyableView(buffer)));
- ASSERT_SAME_TYPE(decltype(comm.begin()), std::common_iterator<int*, sentinel_wrapper<int*>>);
+ CopyableView view{buf, buf + 8};
+ std::ranges::common_view<CopyableView> const common(view);
+ using CommonIter = std::common_iterator<int*, sentinel_wrapper<int*>>;
+ std::same_as<CommonIter> auto begin = common.begin();
+ assert(begin == std::ranges::begin(view));
}
return 0;
// common_view() requires default_initializable<V> = default;
#include <ranges>
+
#include <cassert>
+#include <concepts>
#include "test_iterators.h"
-#include "test_range.h"
-
-int globalBuffer[4] = {1,2,3,4};
-
-struct ContiguousView : std::ranges::view_base {
- int *ptr_;
- constexpr ContiguousView(int* ptr) : ptr_(ptr) {}
- constexpr ContiguousView(ContiguousView&&) = default;
- constexpr ContiguousView& operator=(ContiguousView&&) = default;
- friend constexpr int* begin(ContiguousView& view) { return view.ptr_; }
- friend constexpr int* begin(ContiguousView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
-
-struct CopyableView : std::ranges::view_base {
- int *ptr_;
- constexpr CopyableView(int* ptr = globalBuffer) : ptr_(ptr) {}
- friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
- friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 4};
- }
- friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 4};
- }
-};
-
-struct DefaultConstructibleView : std::ranges::view_base {
- DefaultConstructibleView();
- friend int* begin(DefaultConstructibleView& view);
- friend int* begin(DefaultConstructibleView const& view);
- friend sentinel_wrapper<int*> end(DefaultConstructibleView& view);
- friend sentinel_wrapper<int*> end(DefaultConstructibleView const& view);
-};
+#include "types.h"
int main(int, char**) {
static_assert(!std::default_initializable<std::ranges::common_view<ContiguousView>>);
static_assert( std::default_initializable<std::ranges::common_view<DefaultConstructibleView>>);
- std::ranges::common_view<CopyableView> common;
- assert(*common.begin() == 1);
+ std::ranges::common_view<DefaultConstructibleView> common;
+ assert(common.begin() == nullptr);
+ assert(common.end() == nullptr);
return 0;
}
// constexpr explicit common_view(V r);
#include <ranges>
+
#include <cassert>
+#include <utility>
#include "test_iterators.h"
-#include "test_range.h"
-
-struct ContiguousView : std::ranges::view_base {
- int *ptr_;
- constexpr ContiguousView(int* ptr) : ptr_(ptr) {}
- constexpr ContiguousView(ContiguousView&&) = default;
- constexpr ContiguousView& operator=(ContiguousView&&) = default;
- friend constexpr int* begin(ContiguousView& view) { return view.ptr_; }
- friend constexpr int* begin(ContiguousView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(ContiguousView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
-
-struct CopyableView : std::ranges::view_base {
- int *ptr_;
- constexpr CopyableView(int* ptr) : ptr_(ptr) {}
- friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
- friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
+#include "types.h"
constexpr bool test() {
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
-
- {
- std::ranges::common_view<ContiguousView> common(ContiguousView{buffer});
- assert(std::move(common).base().ptr_ == buffer);
- }
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
{
- ContiguousView v{buffer};
- std::ranges::common_view<ContiguousView> common(std::move(v));
- assert(std::move(common).base().ptr_ == buffer);
+ ContiguousView view{buf, buf + 8};
+ std::ranges::common_view<ContiguousView> common(std::move(view));
+ assert(std::move(common).base().begin_ == buf);
}
{
- const CopyableView v{buffer};
- const std::ranges::common_view<CopyableView> common(v);
- assert(common.base().ptr_ == buffer);
+ CopyableView const view{buf, buf + 8};
+ std::ranges::common_view<CopyableView> const common(view);
+ assert(common.base().begin_ == buf);
}
return true;
test();
static_assert(test());
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
- const std::ranges::common_view<ContiguousView> common(ContiguousView{buffer});
- assert(common.begin() == buffer);
+ // Can't compare common_iterator inside constexpr
+ {
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ ContiguousView view{buf, buf + 8};
+ std::ranges::common_view<ContiguousView> const common(std::move(view));
+ assert(common.begin() == buf);
+ }
return 0;
}
// constexpr auto end() const requires range<const V>;
#include <ranges>
+
#include <cassert>
+#include <concepts>
+#include <utility>
#include "test_macros.h"
#include "test_iterators.h"
-#include "test_range.h"
-
-struct CopyableView : std::ranges::view_base {
- int *ptr_;
- constexpr CopyableView(int* ptr) : ptr_(ptr) {}
- friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
- friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
-
-using ForwardIter = forward_iterator<int*>;
-struct SizedForwardView : std::ranges::view_base {
- int *ptr_;
- constexpr SizedForwardView(int* ptr) : ptr_(ptr) {}
- friend constexpr ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.ptr_); }
- friend constexpr ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.ptr_); }
- friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView& view) {
- return sentinel_wrapper<ForwardIter>{ForwardIter(view.ptr_ + 8)};
- }
- friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView const& view) {
- return sentinel_wrapper<ForwardIter>{ForwardIter(view.ptr_ + 8)};
- }
-};
-
-constexpr auto operator-(sentinel_wrapper<ForwardIter> sent, ForwardIter iter) {
- return sent.base().base() - iter.base();
-}
-constexpr auto operator-(ForwardIter iter, sentinel_wrapper<ForwardIter> sent) {
- return iter.base() - sent.base().base();
-}
-
-using RandomAccessIter = random_access_iterator<int*>;
-struct SizedRandomcAccessView : std::ranges::view_base {
- int *ptr_;
- constexpr SizedRandomcAccessView(int* ptr) : ptr_(ptr) {}
- friend constexpr RandomAccessIter begin(SizedRandomcAccessView& view) { return RandomAccessIter(view.ptr_); }
- friend constexpr RandomAccessIter begin(SizedRandomcAccessView const& view) { return RandomAccessIter(view.ptr_); }
- friend constexpr sentinel_wrapper<RandomAccessIter> end(SizedRandomcAccessView& view) {
- return sentinel_wrapper<RandomAccessIter>{RandomAccessIter(view.ptr_ + 8)};
- }
- friend constexpr sentinel_wrapper<RandomAccessIter> end(SizedRandomcAccessView const& view) {
- return sentinel_wrapper<RandomAccessIter>{RandomAccessIter(view.ptr_ + 8)};
- }
-};
-
-constexpr auto operator-(sentinel_wrapper<RandomAccessIter> sent, RandomAccessIter iter) {
- return sent.base().base() - iter.base();
-}
-constexpr auto operator-(RandomAccessIter iter, sentinel_wrapper<RandomAccessIter> sent) {
- return iter.base() - sent.base().base();
-}
+#include "types.h"
constexpr bool test() {
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
{
- std::ranges::common_view<SizedRandomcAccessView> comm(SizedRandomcAccessView{buffer});
- assert(comm.end().base() == buffer + 8);
- // Note this should NOT be the sentinel type.
- ASSERT_SAME_TYPE(decltype(comm.end()), RandomAccessIter);
+ SizedRandomAccessView view{buf, buf + 8};
+ std::ranges::common_view<SizedRandomAccessView> common(view);
+ std::same_as<RandomAccessIter> auto end = common.end(); // Note this should NOT be the sentinel type.
+ assert(end.base() == buf + 8);
}
+ // const version
{
- const std::ranges::common_view<SizedRandomcAccessView> comm(SizedRandomcAccessView{buffer});
- assert(comm.end().base() == buffer + 8);
- // Note this should NOT be the sentinel type.
- ASSERT_SAME_TYPE(decltype(comm.end()), RandomAccessIter);
+ SizedRandomAccessView view{buf, buf + 8};
+ std::ranges::common_view<SizedRandomAccessView> const common(view);
+ std::same_as<RandomAccessIter> auto end = common.end(); // Note this should NOT be the sentinel type.
+ assert(end.base() == buf + 8);
}
return true;
test();
static_assert(test());
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
-
- using CommonForwardIter = std::common_iterator<ForwardIter, sentinel_wrapper<ForwardIter>>;
- using CommonIntIter = std::common_iterator<int*, sentinel_wrapper<int*>>;
-
- {
- std::ranges::common_view<SizedForwardView> comm(SizedForwardView{buffer});
- assert(comm.end() == CommonForwardIter(sentinel_wrapper<ForwardIter>(ForwardIter(buffer + 8))));
- ASSERT_SAME_TYPE(decltype(comm.end()), CommonForwardIter);
- }
-
- {
- std::ranges::common_view<CopyableView> comm(CopyableView{buffer});
- assert(comm.end() == CommonIntIter(sentinel_wrapper<int*>(buffer + 8)));
- ASSERT_SAME_TYPE(decltype(comm.end()), CommonIntIter);
- }
-
- {
- const std::ranges::common_view<SizedForwardView> comm(SizedForwardView{buffer});
- assert(comm.end() == CommonForwardIter(sentinel_wrapper<ForwardIter>(ForwardIter(buffer + 8))));
- ASSERT_SAME_TYPE(decltype(comm.end()), CommonForwardIter);
- }
-
{
- const std::ranges::common_view<CopyableView> comm(CopyableView{buffer});
- assert(comm.end() == CommonIntIter(sentinel_wrapper<int*>(buffer + 8)));
- ASSERT_SAME_TYPE(decltype(comm.end()), CommonIntIter);
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+ using CommonForwardIter = std::common_iterator<ForwardIter, sentinel_wrapper<ForwardIter>>;
+ using CommonIntIter = std::common_iterator<int*, sentinel_wrapper<int*>>;
+
+ {
+ SizedForwardView view{buf, buf + 8};
+ std::ranges::common_view<SizedForwardView> common(view);
+ std::same_as<CommonForwardIter> auto end = common.end();
+ assert(end == CommonForwardIter(std::ranges::end(view)));
+ }
+ {
+ CopyableView view{buf, buf + 8};
+ std::ranges::common_view<CopyableView> common(view);
+ std::same_as<CommonIntIter> auto end = common.end();
+ assert(end == CommonIntIter(std::ranges::end(view)));
+ }
+
+ // const versions
+ {
+ SizedForwardView view{buf, buf + 8};
+ std::ranges::common_view<SizedForwardView> const common(view);
+ std::same_as<CommonForwardIter> auto end = common.end();
+ assert(end == CommonForwardIter(std::ranges::end(view)));
+ }
+ {
+ CopyableView view{buf, buf + 8};
+ std::ranges::common_view<CopyableView> const common(view);
+ std::same_as<CommonIntIter> auto end = common.end();
+ assert(end == CommonIntIter(std::ranges::end(view)));
+ }
}
return 0;
#include <cassert>
#include "test_iterators.h"
-#include "test_range.h"
+#include "types.h"
-struct CopyableView : std::ranges::view_base {
- int *ptr_;
- constexpr CopyableView(int* ptr) : ptr_(ptr) {}
- friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
- friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
- friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
- friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
- return sentinel_wrapper<int*>{view.ptr_ + 8};
- }
-};
-
-using ForwardIter = forward_iterator<int*>;
-struct SizedForwardView : std::ranges::view_base {
- int *ptr_;
- constexpr SizedForwardView(int* ptr) : ptr_(ptr) {}
- friend constexpr ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.ptr_); }
- friend constexpr ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.ptr_); }
- friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView& view) {
- return sentinel_wrapper<ForwardIter>{ForwardIter(view.ptr_ + 8)};
- }
- friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView const& view) {
- return sentinel_wrapper<ForwardIter>{ForwardIter(view.ptr_ + 8)};
- }
-};
-
-constexpr auto operator-(sentinel_wrapper<ForwardIter> sent, ForwardIter iter) {
- return sent.base().base() - iter.base();
-}
-constexpr auto operator-(ForwardIter iter, sentinel_wrapper<ForwardIter> sent) {
- return iter.base() - sent.base().base();
-}
-
-template<class T>
-concept SizeEnabled = requires(const std::ranges::common_view<T>& comm) {
- comm.size();
-};
+template<class View>
+concept SizeEnabled = requires(View v) { v.size(); };
constexpr bool test() {
- int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
{
- static_assert( SizeEnabled<SizedForwardView>);
- static_assert(!SizeEnabled<CopyableView>);
+ static_assert( SizeEnabled<std::ranges::common_view<SizedForwardView> const&>);
+ static_assert(!SizeEnabled<std::ranges::common_view<CopyableView> const&>);
}
{
- std::ranges::common_view<SizedForwardView> common(SizedForwardView{buffer});
+ SizedForwardView view{buf, buf + 8};
+ std::ranges::common_view<SizedForwardView> common(view);
assert(common.size() == 8);
}
{
- const std::ranges::common_view<SizedForwardView> common(SizedForwardView{buffer});
+ SizedForwardView view{buf, buf + 8};
+ std::ranges::common_view<SizedForwardView> const common(view);
assert(common.size() == 8);
}
--- /dev/null
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_COMMON_VIEW_TYPES_H
+#define TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_COMMON_VIEW_TYPES_H
+
+#include "test_iterators.h"
+
+struct DefaultConstructibleView : std::ranges::view_base {
+ int* begin_ = nullptr;
+ int* end_ = nullptr;
+
+ DefaultConstructibleView() = default;
+ friend constexpr int* begin(DefaultConstructibleView& view) { return view.begin_; }
+ friend constexpr int* begin(DefaultConstructibleView const& view) { return view.begin_; }
+ friend constexpr sentinel_wrapper<int*> end(DefaultConstructibleView& view) {
+ return sentinel_wrapper<int*>(view.end_);
+ }
+ friend constexpr sentinel_wrapper<int*> end(DefaultConstructibleView const& view) {
+ return sentinel_wrapper<int*>(view.end_);
+ }
+};
+
+struct ContiguousView : std::ranges::view_base {
+ int* begin_;
+ int* end_;
+
+ constexpr ContiguousView(int* b, int* e) : begin_(b), end_(e) { }
+ constexpr ContiguousView(ContiguousView&&) = default;
+ constexpr ContiguousView& operator=(ContiguousView&&) = default;
+ friend constexpr int* begin(ContiguousView& view) { return view.begin_; }
+ friend constexpr int* begin(ContiguousView const& view) { return view.begin_; }
+ friend constexpr sentinel_wrapper<int*> end(ContiguousView& view) {
+ return sentinel_wrapper<int*>{view.end_};
+ }
+ friend constexpr sentinel_wrapper<int*> end(ContiguousView const& view) {
+ return sentinel_wrapper<int*>{view.end_};
+ }
+};
+
+struct CopyableView : std::ranges::view_base {
+ int* begin_;
+ int* end_;
+
+ constexpr CopyableView(int* b, int* e) : begin_(b), end_(e) { }
+ friend constexpr int* begin(CopyableView& view) { return view.begin_; }
+ friend constexpr int* begin(CopyableView const& view) { return view.begin_; }
+ friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
+ return sentinel_wrapper<int*>{view.end_};
+ }
+ friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
+ return sentinel_wrapper<int*>{view.end_};
+ }
+};
+
+using ForwardIter = forward_iterator<int*>;
+struct SizedForwardView : std::ranges::view_base {
+ int* begin_;
+ int* end_;
+
+ constexpr SizedForwardView(int* b, int* e) : begin_(b), end_(e) { }
+ friend constexpr ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.begin_); }
+ friend constexpr ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.begin_); }
+ friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView& view) {
+ return sentinel_wrapper<ForwardIter>{ForwardIter(view.end_)};
+ }
+ friend constexpr sentinel_wrapper<ForwardIter> end(SizedForwardView const& view) {
+ return sentinel_wrapper<ForwardIter>{ForwardIter(view.end_)};
+ }
+};
+// Required to make SizedForwardView a sized view.
+constexpr auto operator-(sentinel_wrapper<ForwardIter> sent, ForwardIter iter) {
+ return sent.base().base() - iter.base();
+}
+constexpr auto operator-(ForwardIter iter, sentinel_wrapper<ForwardIter> sent) {
+ return iter.base() - sent.base().base();
+}
+
+using RandomAccessIter = random_access_iterator<int*>;
+struct SizedRandomAccessView : std::ranges::view_base {
+ int* begin_;
+ int* end_;
+
+ constexpr SizedRandomAccessView(int* b, int* e) : begin_(b), end_(e) { }
+ friend constexpr RandomAccessIter begin(SizedRandomAccessView& view) { return RandomAccessIter(view.begin_); }
+ friend constexpr RandomAccessIter begin(SizedRandomAccessView const& view) { return RandomAccessIter(view.begin_); }
+ friend constexpr sentinel_wrapper<RandomAccessIter> end(SizedRandomAccessView& view) {
+ return sentinel_wrapper<RandomAccessIter>{RandomAccessIter(view.end_)};
+ }
+ friend constexpr sentinel_wrapper<RandomAccessIter> end(SizedRandomAccessView const& view) {
+ return sentinel_wrapper<RandomAccessIter>{RandomAccessIter(view.end_)};
+ }
+};
+// Required to make SizedRandomAccessView a sized view.
+constexpr auto operator-(sentinel_wrapper<RandomAccessIter> sent, RandomAccessIter iter) {
+ return sent.base().base() - iter.base();
+}
+constexpr auto operator-(RandomAccessIter iter, sentinel_wrapper<RandomAccessIter> sent) {
+ return iter.base() - sent.base().base();
+}
+
+#endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_COMMON_VIEW_TYPES_H