Skip to content

Commit

Permalink
Fixes const iteration for chain()
Browse files Browse the repository at this point in the history
Fixes #65
  • Loading branch information
ryanhaining committed Dec 23, 2019
1 parent 334c715 commit cb36354
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 12 additions & 2 deletions chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ namespace iter {
// rather than a chain function, use a callable object to support
// from_iterable
class ChainMaker;

template <typename>
struct AsTupleOfConstImpl;

template <typename... Ts>
struct AsTupleOfConstImpl<std::tuple<Ts...>>
: type_is<std::tuple<AsConst<Ts>...>> {};

template <typename T>
using AsTupleOfConst = typename AsTupleOfConstImpl<T>::type;
}
}

Expand Down Expand Up @@ -169,12 +179,12 @@ class iter::impl::Chained {
{get_end(std::get<Is>(tup_))...}};
}

Iterator<AsConst<TupType>> begin() const {
Iterator<AsTupleOfConst<TupType>> begin() const {
return {0, {get_begin(std::as_const(std::get<Is>(tup_)))...},
{get_end(std::as_const(std::get<Is>(tup_)))...}};
}

Iterator<AsConst<TupType>> end() const {
Iterator<AsTupleOfConst<TupType>> end() const {
return {sizeof...(Is), {get_end(std::as_const(std::get<Is>(tup_)))...},
{get_end(std::as_const(std::get<Is>(tup_)))...}};
}
Expand Down
14 changes: 7 additions & 7 deletions test/test_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "catch.hpp"

using iter::chain;
using itertest::SolidInt;
using itertest::BasicIterable;
using itertest::SolidInt;
using Vec = const std::vector<char>;

TEST_CASE("chain: three strings", "[chain]") {
Expand All @@ -28,8 +28,8 @@ TEST_CASE("chain: three strings", "[chain]") {

TEST_CASE("chain: const iteration", "[chain][const]") {
std::string s1{"abc"};
/* const */ std::string s2{"mno"};
auto ch = chain(s1, s2, std::string{"xyz"});
const std::string s2{"mno"};
const auto ch = chain(s1, s2, std::string{"xyz"});

Vec v(std::begin(ch), std::end(ch));
Vec vc{'a', 'b', 'c', 'm', 'n', 'o', 'x', 'y', 'z'};
Expand Down Expand Up @@ -309,8 +309,8 @@ template <typename T>
using ImpT2 = decltype(chain.from_iterable(std::declval<T>()));
TEST_CASE("chain.from_iterable: has correct ctor and assign ops",
"[chain.from_iterable]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT2<std::vector<std::string>>>::
value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT2<std::vector<std::string>&>>::
value);
REQUIRE(itertest::IsMoveConstructibleOnly<
ImpT2<std::vector<std::string>>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<
ImpT2<std::vector<std::string>&>>::value);
}

0 comments on commit cb36354

Please sign in to comment.