I wanted to create a deep_flatten
function template that would produce a range
of elements that are deeply join
ed. For example, if we take into account only nested std::vector
s, I can have:
template <typename T>
struct is_vector : public std::false_type { };
template <typename T, typename A>
struct is_vector<std::vector<T, A>> : public std::true_type { };
template <typename T>
auto deepFlatten(const std::vector<std::vector<T>>& vec) {
using namespace std::ranges;
if constexpr (is_vector<T>::value) {
auto range = vec | views::join;
return deepFlatten(std::vector(range.begin(), range.end()));
} else {
auto range = vec | views::join;
return std::vector(range.begin(), range.end());
}
}
This enables me to do:
std::vector<std::vector<std::vector<int>>> nested_vectors = {
{{1, 2, 3}, {4, 5}, {6}},
{{7}, {8, 9}, {10, 11, 12}},
{{13}}
};
std::ranges::copy(
deep_flatten(nested_vectors),
std::ostream_iterator<int>(std::cout, " ")
);
which prints into the console the following text, as expected:
1 2 3 4 5 6 7 8 9 10 11 12 13
But, I don't like this solution that much. Not only it's inefficient (creating a number of temporary vectors), but it also works only with std::vector
s. I figured that I could use some more of c++20 magic and use std::ranges::range
concept:
namespace rng {
template <std::ranges::range Rng>
auto deep_flatten(Rng&& rng) {
using namespace std::ranges;
if constexpr (range<Rng>) {
return deep_flatten(rng | views::join);
} else {
return rng | views::join;
}
}
}
This seemed to me pretty straightforward - we have a std::ranges::range
and we inspect it's value type. Depending on whether it's a nested range, we recurse or simply return join
ed elements.
Sadly, it doesn't work. After trying to run:
int main() {
std::set<std::vector<std::list<int>>> nested_ranges = {
{{1, 2, 3}, {4, 5}, {6}},
{{7}, {8, 9}, {10, 11, 12}},
{{13}}
};
std::ranges::copy(
rng::deep_flatten(nested_ranges),
std::ostream_iterator<int>(std::cout, " ")
);
}
I get an error saying that:
In instantiation of 'auto rng::deep_flatten(Rng&&) [with Rng = std::ranges::join_view<std::ranges::ref_view<std::set<std::vector<std::__cxx11::list<int> > > > >]': required from 'auto rng::deep_flatten(Rng&&) [with Rng = std::set<std::vector<std::__cxx11::list<int> > >&]' required from here error: use of 'auto rng::deep_flatten(Rng&&) [with Rng = std::ranges::join_view<std::ranges::ref_view<std::set<std::vector<std::__cxx11::list<int> > > > >]' before deduction of 'auto' 39 | return deep_flatten(rng | views::join); | ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
Having researched similar problems, I cannot really get why the error appears here.
I am using gcc version 10.1.0 (Rev3, Built by MSYS2 project)
There are two problems here.
The first problem is yours:
namespace rng {
template <std::ranges::range Rng>
auto deep_flatten(Rng&& rng) {
using namespace std::ranges;
if constexpr (range<Rng>) { // <==
return deep_flatten(rng | views::join);
} else {
return rng | views::join;
}
}
}
This function is infinitely recursive. deep_flatten
is constrained range<Rng>
, so the if constexpr
check there is always going to be true, so we're never going to enter the base case. This is just a bug - we're checking the wrong thing, it's not if we're a range, it's if our underlying value is a range. That's:
namespace rng {
template <typename Rng>
auto deep_flatten(Rng&& rng) {
using namespace std::ranges;
auto joined = rng | views::join;
if constexpr (range<range_value_t<decltype(joined)>>) {
return deep_flatten(joined);
} else {
return joined;
}
}
}
And here we get into the second problem, which is the standard library's problem. What rng | views::join
means is:
The name
views::join
denotes a range adaptor object ([range.adaptor.object]). Given a subexpressionE
, the expressionviews::join(E)
is expression-equivalent tojoin_view{E}
.
But join_view{E}
for an E
that's already a specialization of join_view
... is a no-op right now because of class template argument deduction (CTAD) - the copy deduction candidate is the best candidate, so our nested join
operation actually becomes a single join
. Your original implementation gets around this problem because it's not join
-ing a join_view
, it's always join
-ing vector
s.
I've submitted LWG 3474.
In the meantime, we can work around the views::join
problem by just directly using join_view
and specifying the template argument explicitly:
namespace rng {
template <typename Rng>
auto deep_flatten(Rng&& rng) {
using namespace std::ranges;
auto joined = join_view<views::all_t<Rng>>(rng);
if constexpr (range<range_value_t<decltype(joined)>>) {
return deep_flatten(joined);
} else {
return joined;
}
}
}
This works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With