I'm trying to join two views:
#include <ranges>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector v{1,2,3,4,5,6,7,4,8,9};
auto view_filtered = v | std::views::filter([](const int val){return val %2 == 0;});
vector v_10{10,20,30,40,50,60,70,80,90};
auto view_transformed = v_10 | std::ranges::views::transform([](auto val){return val * 10;});
auto join_views =view_transformed | std::ranges::views::join(view_filtered);
return 0;
}
Can somebody tell me why is this not the correct way? Thank you
Edit:
main.cpp: In function ‘int main()’: main.cpp:24:65: error: no match for call to ‘(const std::ranges::views::_Join) (std::ranges::filter_view > >, main():: >&)’ 24 | auto join_views =view_transformed | std::ranges::views::join(view_filtered);
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~ In file included from main.cpp:9: /usr/include/c++/11/ranges:2785:9: note: candidate: ‘template requires (viewable_range<_Range>) && (__can_join_view<_Range>) constexpr auto std::ranges::views::_Join::operator()(_Range&&) const’ 2785 | operator()(_Range&& __r) const
| ^~~~~~~~ /usr/include/c++/11/ranges:2785:9: note: template argument deduction/substitution failed: /usr/include/c++/11/ranges:2785:9: note: constraints not satisfied /usr/include/c++/11/ranges: In substitution of ‘template<class _Range> requires (viewable_range<_Range>) && (__can_join_view<_Range>) constexpr auto std::ranges::views::_Join::operator()(_Range&&) const [with _Range = std::ranges::filter_view<std::ranges::ref_view<std::vector<int, std::allocator<int> > >, main()::<lambda(int)> >&]’: main.cpp:24:65: required from here /usr/include/c++/11/ranges:2776:10: required for the satisfaction of ‘__can_join_view<_Range>’ [with _Range = std::ranges::filter_view<std::ranges::ref_view<std::vector<int, std::allocator<int> > >, main::._anon_103>&] /usr/include/c++/11/ranges:2777:6: in requirements [with _Range = std::ranges::filter_view<std::ranges::ref_view<std::vector<int, std::allocator<int> > >, main::._anon_103>&] /usr/include/c++/11/ranges:2777:24: note: the required expression ‘std::ranges::join_view >{declval<_Range>()}’ is invalid 2777 |
= requires { join_view<all_t<_Range>>{std::declval<_Range>()}; };
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1plus: note: set ‘-fconcepts-diagnostics-depth=’ to at least 2 for more detail
join
is for joining elements of a range into a single one, e.g. characters into a string. What you're looking for is concatenation, unfortunately not available in C++ stdlib (yet at least).range-v3 provides it
auto join_views = ranges::views::concat(view_filtered, view_transformed);
See: https://stackoverflow.com/a/62456584/4885321Demo: https://godbolt.org/z/6T51T3b4f