[range] Trying to extend boost::range::combine to std::tuples

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[range] Trying to extend boost::range::combine to std::tuples

Boost - Dev mailing list
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)...));
}

}

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [range] Trying to extend boost::range::combine to std::tuples

Boost - Dev mailing list
On Fri, 6 Dec 2019 at 08:55, Andres Llopis via Boost
<[hidden email]> 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>().

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [range] Trying to extend boost::range::combine to std::tuples

Boost - Dev mailing list
> 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?

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [range] Trying to extend boost::range::combine to std::tuples

Boost - Dev mailing list
On Fri, 6 Dec 2019, 14:44 Andres Llopis, <[hidden email]> 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:
>
> https://godbolt.org/z/wGJCp-
>
> 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.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [range] Trying to extend boost::range::combine to std::tuples

Boost - Dev mailing list
On 2019-12-06 18:09, Mathias Gaunard via Boost wrote:

> On Fri, 6 Dec 2019, 14:44 Andres Llopis, <[hidden email]> 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:
>>
>> https://godbolt.org/z/wGJCp-
>>
>> 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

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [range] Trying to extend boost::range::combine to std::tuples

Boost - Dev mailing list
On Fri, 6 Dec 2019 at 16:40, Andrey Semashev via Boost
<[hidden email]> wrote:

> They do, since C++14, but only if the type is specified exactly once in
> the list of types in the tuple.

Oh I didn't know about this, sorry.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost