From ebd88b25e7284cbb293350d6898512ff89d5745d Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 24 Feb 2020 14:39:58 +1100 Subject: [PATCH] iterator/zip: use the `iterable` concept for zip construction --- iterator/zip.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/iterator/zip.hpp b/iterator/zip.hpp index e5a4e095..cc07d2d0 100644 --- a/iterator/zip.hpp +++ b/iterator/zip.hpp @@ -9,6 +9,7 @@ #pragma once #include "iota.hpp" +#include "../concepts.hpp" #include "../tuple/value.hpp" #include "../variadic.hpp" #include "../functor.hpp" @@ -81,6 +82,21 @@ namespace cruft::iterator { } + decltype (auto) + operator* (void) const + { + // We deliberately construct a tuple manually here to reduce + // the risk of dangling references. `forward_as_tuple` and + // `make_tuple` have resulted in references to locals in the + // past. + return std::tuple< + decltype(*std::get (m_iterators))... + > ( + *std::get (m_iterators)... + ); + } + + bool operator== (const zipped_iterator &rhs) const { @@ -178,6 +194,7 @@ namespace cruft::iterator { /// /// eg, cruft::zip ({1,2,3}, {4,5,6}) ~= {{1,4},{2,5},{3,6}} template + requires (cruft::concepts::iterable && ...) decltype(auto) zip (ContainerT&&... data) { @@ -199,6 +216,7 @@ namespace cruft::iterator { /// /// eg, cruft::izip ("abc") ~= {{0,'a'},{1,'b'},{2,'c'}} template + requires (cruft::concepts::iterable && ...) decltype(auto) izip (ContainerT&&... data) {