diff --git a/unit_test/view_gtest.cpp b/unit_test/view_gtest.cpp index 0e17ce74..59286efe 100644 --- a/unit_test/view_gtest.cpp +++ b/unit_test/view_gtest.cpp @@ -244,6 +244,27 @@ TEST(viewTest, viewTransformAsOutput) EXPECT_EQ(-2, test[1].second); } +constexpr StdArrInt2 generatedArray() +{ + StdArrInt2 res{}; + int i{1}; + auto v = view::generate([&i] { return i++; }, 2); + auto it = v.begin(); + for (auto & val : res) + { + val = *it; + it++; + } + return res; +} + +constexpr void testViewGenerateConstexpr() +{ + constexpr auto res = generatedArray(); + static_assert(res[0] == 1); + static_assert(res[1] == 2); +} + struct Ints { int i; @@ -285,7 +306,7 @@ TEST(viewTest, viewMoveMutableEmptyAndSize) } using IntGenIter = oel::iterator_t; -static_assert(std::input_or_output_iterator); +static_assert(std::input_iterator); TEST(viewTest, chainWithStd) { diff --git a/view/generate.h b/view/generate.h index fe7ec4ac..c9f90b79 100644 --- a/view/generate.h +++ b/view/generate.h @@ -16,9 +16,9 @@ namespace oel { template< typename Generator > -class _generateIterator +class generate_iterator { - typename _detail::AssignableWrap::Type _g; + typename _detail::AssignableWrap::Type mutable _g; public: using iterator_category = std::input_iterator_tag; @@ -27,14 +27,14 @@ class _generateIterator using value_type = std::remove_cv_t< std::remove_reference_t >; using difference_type = ptrdiff_t; - constexpr explicit _generateIterator(Generator g) : _g{std::move(g)} {} + constexpr explicit generate_iterator(Generator g) : _g{std::move(g)} {} - constexpr reference operator*() + constexpr reference operator*() const { return static_cast(_g)(); } - constexpr _generateIterator & operator++() OEL_ALWAYS_INLINE { return *this; } + constexpr generate_iterator & operator++() OEL_ALWAYS_INLINE { return *this; } constexpr void operator++(int) & OEL_ALWAYS_INLINE {} }; @@ -45,7 +45,7 @@ namespace view /** * Like `generate_n` in the Range-v3 library, but this is only for use within OE-Lib. */ inline constexpr auto generate = - [](auto generator, ptrdiff_t count) { return counted(_generateIterator{std::move(generator)}, count); }; + [](auto generator, ptrdiff_t count) { return counted(generate_iterator{std::move(generator)}, count); }; } } // oel