A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from
Questions tagged [std-ranges]
379 questions
7
votes
1 answer
Difference between lambda and member function pointer
In my answer here, Barry pointed out that it's better to call views::transform(&Planter::getPlants) because views::transform([](Planter const& planter){... accidentally copies.
#if 1
auto plants = planters
|…

Tom Huntington
- 2,260
- 10
- 20
7
votes
2 answers
Regarding the pause-resume data loss in MSVC std::experimental::generator
Since the std::generator is making it into CPP23, I am playing around with MSVC's incomplete version.
However, I notice that it seems lose exactly one yield when used with std::views::take. Here is the example:
#include
#include…

Hydrogen
- 301
- 1
- 8
7
votes
1 answer
When should i use range adaptors vs range algorithms?
Much of the functionality provided by the various range adaptors seem to be quite similar to the algorithms library.
For example std::ranges::transform_view
A range adaptor that represents view of an underlying sequence after
applying a…

Cortex0101
- 831
- 2
- 12
- 28
7
votes
1 answer
Why does my function only work with lvalues?
I have a function that returns a lowercase string:
constexpr auto string_to_lower_case(const std::string& string) {
return string
| std::views::transform(std::tolower)
| std::views::transform([](const auto& ascii) { return…

camelopard
- 83
- 1
- 5
7
votes
1 answer
Why do `std::ranges::size` require a non-const method when using ADL?
Otherwise, size(t) converted to its decayed type, if ranges::disable_sized_range> is false, and the converted expression is valid and has an integer-like type, where the overload resolution is performed with the following…

zjyhjqs
- 609
- 4
- 11
7
votes
3 answers
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
#include
#include
int main()
{
std::vector foo{1, 2, 3, 4, 5, 6};
for (const auto& d: foo |…

cr0pp
- 73
- 3
7
votes
1 answer
Iterating over first n elements of a container - std::span vs views::take vs ranges::subrange
So with c++ 20 we get a lot of new features with ranges, spans and so on. Now if i need to iterate over a container, but only the first n elements, what would be the most appropriate way and is there any practical difference going on behind the…

Jane Doe
- 480
- 3
- 15
7
votes
1 answer
Why doesn't ranges provide a type erased view for non-contiguous ranges?
I would like to have a function that will take any range/view of a fixed value type.
int main()
{
std::array, 2> a{...};
std::array, 3> b{...};
generic_fun(a);
generic_fun(b);
};
Of course I…

Tom Huntington
- 2,260
- 10
- 20
7
votes
1 answer
Undefined behaviour on std::prev for transform-view
Consider the following code (click here for godbolt):
#include
#include
#include
int main() {
auto v = std::vector{1, 2};
auto view = v | std::views::transform([] (auto i) { return static_cast(i);…

Emma X
- 257
- 3
- 10
7
votes
1 answer
C++why do constrained algorithms (e.g. std::ranges::merge) also return the end of the input ranges?
std::ranges::merge (for example) returns a bundle of iterators containing the end of the merged range, obviously, but also the end of the two input ranges. Cppreference says (https://en.cppreference.com/w/cpp/algorithm/ranges)
Additionally, the…

le migou
- 471
- 2
- 7
7
votes
1 answer
What is the difference between the split_view and the lazy_split_view in C++?
I have read the latest draft where lazy_split_view is added.
But later on, I realized that split_view was renamed into lazy_split_view, and the split_view was renewed.
libstdc++ also recently implemented this by using GCC Trunk version…

Desmond Gold
- 1,517
- 1
- 7
- 19
7
votes
2 answers
Can can I make `std::ranges::views::elements` work with a range of my type
Consider a Point type with x, y and z values. If I have a range of Point objects, such as std::vector, what do I need to add to Point to make it work with the std::ranges::views::elements range adaptor?
The intention is to do something…

darcamo
- 3,294
- 1
- 16
- 27
7
votes
2 answers
Do C++ ranges support projections in views?
I know algorithms (e.g. sort) in ranges support projection, but it seems to me that there is no way to get that functionality for views...
Am I right?
As an example consider following working code:
#include
#include
#include…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
7
votes
0 answers
Why does view_interface::data const overload need an extra check that const D satisfies ranges::range?
According to [view.interface], non-const and const overloaded data functions are defined as follows:
template
requires is_class_v && same_as>
class view_interface : public view_base {
private:
constexpr D&…

康桓瑋
- 33,481
- 5
- 40
- 90
7
votes
2 answers
Why does `iota(0) | take(0)` not model ranges::sized_range in C++20?
Consider the following code snippet:
#include
auto r = std::views::iota(0) | std::views::take(0);
static_assert(std::ranges::sized_range);
gcc-trunk rejects it for required-expression std::ranges::size(r) is invalid. Why does…

康桓瑋
- 33,481
- 5
- 40
- 90