[range] Trying to extend boost::range::combine to std::tuples
Hi everyone, I am trying to make my own version of combine so that it works also with std::tuples. Someone on slack suggested that the zip iterator should be able to handle it properly. My attempt however fails when trying to use the metafunction tuple_of_references::type for the std::tuple of iterators. This is what I tried (https://godbolt.org/z/Ymjtvn): (Please excuse any style problems, this is my first post) namespace myns { using boost::iterators::zip_iterator; using boost::iterator_range; template<typename IterTuple> class combined_range : public iterator_range<zip_iterator<IterTuple> > { typedef iterator_range<zip_iterator<IterTuple> > base; public: combined_range(IterTuple first, IterTuple last) : base(first, last) { } }; template<typename... Ranges> auto combine(Ranges&&... rngs) -> combined_range<decltype(std::make_tuple(std::begin(rngs)...))> { return combined_range<decltype(std::make_tuple(std::begin(rngs)...))>( std::make_tuple(std::begin(rngs)...), std::make_tuple(std::end(rngs)...)); } }
On Fri, 6 Dec 2019 at 08:55, Andres Llopis via Boost <boost@lists.boost.org> wrote:
Hi everyone,
I am trying to make my own version of combine so that it works also with std::tuples. Someone on slack suggested that the zip iterator should be able to handle it properly. My attempt however fails when trying to use the metafunction tuple_of_references::type for the std::tuple of iterators. This is what I tried (https://godbolt.org/z/Ymjtvn):
zip_iterator works with fusion sequences. Make sure you include boost/fusion/adapted/std_tuple.hpp Also std::get<N>(x), not x.get<N>().
zip_iterator works with fusion sequences. Make sure you include boost/fusion/adapted/std_tuple.hpp
Also std::get<N>(x), not x.get<N>().
Thanks Mathias, including fusion adapted std tuple kind of works, only remaining issue is that I cannot use the std::get<> overloads with a type instead of an int: https://godbolt.org/z/wGJCp- What could be the problem?
On Fri, 6 Dec 2019, 14:44 Andres Llopis, <allopis@protonmail.com> wrote:
zip_iterator works with fusion sequences. Make sure you include boost/fusion/adapted/std_tuple.hpp
Also std::get<N>(x), not x.get<N>().
Thanks Mathias, including fusion adapted std tuple kind of works, only remaining issue is that I cannot use the std::get<> overloads with a type instead of an int:
What could be the problem?
Tuples do not support this operation; conceptually it makes no sense. You can write code that finds the first element of a tuple of a certain type, but you can have arbitrary amounts of elements of that type. In any case this has nothing to do with the problem you originally posted.
On 2019-12-06 18:09, Mathias Gaunard via Boost wrote:
On Fri, 6 Dec 2019, 14:44 Andres Llopis, <allopis@protonmail.com> wrote:
zip_iterator works with fusion sequences. Make sure you include boost/fusion/adapted/std_tuple.hpp
Also std::get<N>(x), not x.get<N>().
Thanks Mathias, including fusion adapted std tuple kind of works, only remaining issue is that I cannot use the std::get<> overloads with a type instead of an int:
What could be the problem?
Tuples do not support this operation; conceptually it makes no sense.
They do, since C++14, but only if the type is specified exactly once in the list of types in the tuple. The problem seems to be that the tuple returned by zip_iterator contains references returned after dereferencing the adopted iterators, and you are trying to get a value. Getting references works: https://godbolt.org/z/rdVQGb
participants (3)
-
Andres Llopis
-
Andrey Semashev
-
Mathias Gaunard