I was messing around with a wrapper class for "structure of arrays" and ended up finding "boost-iterators's" zip-iterator. Now I was wondering if there's a way to make the resulting tuple from iterating, work with structured bindings like in the commented code segment below. I found this post boost::combine, range-based for and structured bindings but that didn't help me solve it.
#include <iostream>
#include <numeric>
#include <vector>
#include <boost/iterator/zip_iterator.hpp>
using namespace std;
template <typename T>
struct Buffer2 final
{
vector<T> xs, ys;
auto begin() noexcept
{
return boost::make_zip_iterator(boost::make_tuple(xs.begin(), ys.begin()));
}
auto cbegin() noexcept
{
return boost::make_zip_iterator(boost::make_tuple(xs.cbegin(), ys.cbegin()));
}
auto end() noexcept
{
return boost::make_zip_iterator(boost::make_tuple(xs.end(), ys.end()));
}
auto cend() noexcept
{
return boost::make_zip_iterator(boost::make_tuple(xs.cend(), ys.cend()));
}
};
int main()
{
Buffer2<float> values;
values.xs.resize(10);
values.ys.resize(10);
iota(values.xs.begin(), values.xs.end(), 0);
iota(values.ys.begin(), values.ys.end(), 10);
for (auto value : values)
{
cout << value.get<0>() << ' ' << value.get<1>() << '\n';
}
// This does not work ( x is head_type, y is tail_type )
/* for(auto [x, y] : values) {
cout << x << ' ' << y << '\n';
}*/
}