A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from
Questions tagged [std-ranges]
379 questions
3
votes
0 answers
Convert iterator-sentinel pair to range in clang
I'd like to call a function that takes a single range parameter, using an iterator-sentinel pair. The recommended method seems to be to call std::ranges::subrange(begin, end). For example, I would use the following:
#include
#include…

Mark
- 1,306
- 13
- 19
3
votes
1 answer
How to write an infinite sequence compatible with std::ranges?
I would like to write a struct which will generate an infinite sequence of fibonacci numbers in a way compatible with std::ranges and range adaptors.
So if I wanted the first 5 even fibonacci numbers, I would write something like this:
#include…

Dorian
- 490
- 2
- 10
3
votes
0 answers
Is C++20 lazy_split_view overconstrained Pattern when V is an input_range?
In [range.lazy.split.view], the synopsis of lazy_split_view (the original C++20 split_view before P2210) is defined as follows:
template
requires view && view &&
…

康桓瑋
- 33,481
- 5
- 40
- 90
3
votes
1 answer
Vectors do not satisfy std::ranges::contiguous_range in Eigen 3.4
Why does Eigen::VectorXd not satisfy the concept std::ranges::contiguous_range? That is, static_assert(std::ranges::contiguous_range);
does not compile.
Also, is there the possibility to specialize a template to make Eigen vectors…

fdev
- 127
- 12
3
votes
1 answer
std::views has not been declared
I am trying to use the ranges library from c++20 and I have this simple loop.
for (const int& num : vec | std::views::drop(2)) {
std::cout << num << ' ';
}
I get an error message saying error: 'std::views' has not been declared. I don't get any…

wyvern
- 43
- 1
- 6
3
votes
1 answer
Check if there's duplicate element in an container in compile time with C++20
One simple way of doing this prior to C++20 is to do a nested loop:
template
constexpr bool has_duplicate(Container&& container)
{
for (auto it1 = container.cbegin(); it1 != container.cend(); ++it1)
for(auto it2 =…

Ranoiaetep
- 5,872
- 1
- 14
- 39
3
votes
1 answer
Is there an efficient way to use views::filter after transform? (range adaptors)
A common example of strange behaviour with views::filter:
#include
#include
#include
int main ()
{
using namespace std;
auto ml = [](char c) // ml = make lambda (always accepts / transforms to 1)
{
…

Elliott
- 2,603
- 2
- 18
- 35
3
votes
1 answer
missing `typename` in gcc 11.1.0 ranges header
I'm using clang-12.0.1 and libstdc++ from gcc-11.1.0. When including , I get the following error:
[build] /usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/ranges:3392:19: error: missing 'typename' prior to…

Zuodian Hu
- 979
- 4
- 9
3
votes
2 answers
Iterate over equal ranges of range with C++20
I am wondering if C++20 ranges have some nice way for me to iterate over the equal ranges of sorted container(or in general case any sorted range).
I have this "manual" solution that works, but it is not nice in a sense that it is not really…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
3
votes
1 answer
Compilers disagree on accepting this view::keys code
Out of 3 compilers only gcc accepts this code. I guess clang problem is that it uses antique libstdc++, but MSVC and GCC should support ranges code.
What is the standard mandated…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
3
votes
0 answers
What are the incentive of some std::ranges::XXX_n?
Looking through the algorithm library, there are a few std::ranges::XXX_n function have basically the same definition as their std::XXX_n counterparts, namely copy_n, fill_n, and generate_n.
The only difference between them are the ranges version…

Ranoiaetep
- 5,872
- 1
- 14
- 39
3
votes
1 answer
The real purpose of C++20 keys_view and values_view?
C++20 introduced ranges::elements_view, which accepts a view of tuple-like values, and issues a view with a value-type of the Nth element of the adapted view's value-type, where N is the non-type template parameter.
In [range.elements.view], the…

康桓瑋
- 33,481
- 5
- 40
- 90
3
votes
3 answers
How to check whether elements of a range should be moved?
There's a similar question: check if elements of a range can be moved?
I don't think the answer in it is a nice solution. Actually, it requires partial specialization for all containers.
I made an attempt, but I'm not sure whether checking…

zjyhjqs
- 609
- 4
- 11
3
votes
3 answers
Under what circumstances is ref_view{E} ill-formed and subrange{E} not?
C++20 introduces views::all which is a range adaptor that returns a view that includes all elements of its range argument.
The expression views::all(E) is expression-equivalent (has the same effect) to:
decay-copy(E) if the decayed type of E models…

康桓瑋
- 33,481
- 5
- 40
- 90
3
votes
2 answers
Why ranges::single_view use curly brace initialization for underlying value?
According to [range.single.view#3], one of std::ranges::single_view constructors define as:
template
requires constructible_from
constexpr explicit single_view(in_place_t, Args&&... args);
Effects: Initializes…

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