Is const broken with std::views?

void foo(const auto& collection) { *collection.begin() = 104; } int main() { std::vector<int> ints {1, 2, 3, 4, 5}; foo(ints); // Error, as it should be foo(ints | std::views::all); // Compiles and modifies the vector. Why? return 0; } Why is constness of lvalue reference completely ignored if an argument of a function is of… Read More Is const broken with std::views?

Is there any standard functionality for creating a flattened view of a map with a container as the mapped_type?

Is there any standard functionality to create a range/view over all pairs? The following code illustrates the view I am looking to create: std::unordered_map<std::string, std::vector<int>> m{{"Foo", {1,2}}, {"Hello", {4,5}}}; auto view = ???; std::vector<std::pair<std::string, int>> v{view.begin(), view.end()}; std::vector<std::pair<std::string, int>> out1{{"Foo", 1}, {"Foo", 2}, {"Hello", 4}, {"Hello", 5}}; std::vector<std::pair<std::string, int>> out2{{"Hello", 4}, {"Hello", 5}, {"Foo", 1},… Read More Is there any standard functionality for creating a flattened view of a map with a container as the mapped_type?

What is the best way to drop last element using c++20 ranges

Is there any better way to drop last element in container using c++20 ranges than reverse it twice? #include <iostream> #include <vector> #include <ranges> int main() { std::vector<int> foo{1, 2, 3, 4, 5, 6}; for (const auto& d: foo | std::ranges::views::reverse | std::ranges::views::drop(1) | std::ranges::views::reverse) { std::cout << d << std::endl; } } >Solution :… Read More What is the best way to drop last element using c++20 ranges

std::ranges::remove still not suported / broken on clang trunk when using libstdc++?

Works fine on gcc trunk, but not on clang trunk, both with libstd++. Or am I missing something exceedingly obvious? Godbolt #include <algorithm> #include <iostream> #include <ostream> #include <vector> std::ostream& operator<<(std::ostream& os, const std::vector<int>& v) { for (auto&& e: v) os << e << " "; return os; } int main() { auto ints =… Read More std::ranges::remove still not suported / broken on clang trunk when using libstdc++?

Range transformations with stateful lambdas and std::views::drop

it’s my first time digging into the new <ranges> library and I tried a little experiment combining std::views::transform with a stateful lambda and ‘piping’ the resulting range to std::views::drop: #include <iostream> #include <ranges> #include <vector> using namespace std; int main() { auto aggregator = [sum = 0](int val) mutable { return sum += val; };… Read More Range transformations with stateful lambdas and std::views::drop

Inconsistent behavior with `empty` std ranges view depending on type

Consider the following code snippet: #include <vector> #include <ranges> #include <iostream> struct A { }; struct B { B(void *) {}; }; template<class T, class R> std::vector<T> foo(R &&range) { return std::vector<T> { std::ranges::begin(range), std::ranges::end(range) }; } int main() { std::cout << foo<A>(std::views::empty<A>).size() << "\n"; std::cout << foo<B>(std::views::empty<B>).size() << "\n"; } It correctly prints 0… Read More Inconsistent behavior with `empty` std ranges view depending on type