Questions tagged [range-v3]

range-v3 is a range library for C++14/17/20.

range-v3 is a range library for C++14/17/20. It is the basis of a formal proposal (N4128 Ranges for the Standard Library) to add range support to the C++ standard library. It also is the reference implementation for a Technical Specification (N4560 Working Draft, C++ extensions for Ranges). Source is available on Github.

313 questions
13
votes
3 answers

Using ranges::view::iota in parallel algorithms

Since there is no index based parallel for algorithm in c++17, I'm wondering if ranges::view::iota can be used in combination with std::for_each to emulate that. That is: using namespace std; constexpr int N= 10'000'000; ranges::iota_view…
metalfox
  • 6,301
  • 1
  • 21
  • 43
13
votes
1 answer

Why does range-v3 put its function objects into an inline namespace?

In range-v3, all of the functions are really global function objects in an inline namespace: #if RANGES_CXX_INLINE_VARIABLES < RANGES_CXX_INLINE_VARIABLES_17 #define RANGES_INLINE_VARIABLE(type, name) \ inline…
Barry
  • 286,269
  • 29
  • 621
  • 977
13
votes
1 answer

Is a container sure to be a range conceptually?

From the documentation of ranges-v3: view::all Return a range containing all the elements in the source. Useful for converting containers to ranges. What makes me confused are: Under what scenarios are view::all used? Are standard containers…
szxwpmj
  • 465
  • 2
  • 10
13
votes
1 answer

What's the difference between a "range" and a "view" in the rangesv3 ts?

What's the difference between a "range" and a "view" in the rangesv3 ts? Don't find any similar answers on G search. Guess I'm struggling with a basic overview of what each one is supposed to do: is it the case (in c++ speak) that range 'is-a' view,…
r webby
  • 443
  • 3
  • 13
12
votes
1 answer

In ranges-v3, how do I create a range from a pair of iterators?

How do I create a ranges-v3-compatible range, given a traditional pair of "begin" and "end" iterators? Let's say that I am writing a generic function that accepts two iterators, for compatibility with legacy code. struct result; bool…
NicholasM
  • 4,557
  • 1
  • 20
  • 47
12
votes
2 answers

Why is ranges::ostream_iterator default-constructible?

This question follows a discussion in the comments here. In Eric Niebler's ranges-v3 library (which is sort-of becoming part of the standard for C++20), ranges::ostream_iterator is default-constructible - without an ostream. How come? I thought…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
12
votes
1 answer

Does view::join require copyable inner range? Why?

Suppose that we have cppcoro::generator gen_impl(int in) { const auto upper = in + 10; for (; in < upper; ++in) co_yield in; } cppcoro::generator> gen() { for (int n = 1; n < 100; n += 10) co_yield…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
11
votes
3 answers

Why is std::common_iterator just std::forward_iterator?

C++20 introduced a std::common_iterator that is capable of representing a non-common range of elements (where the types of the iterator and sentinel differ) as a common range (where they are the same), its synopsis defines…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
10
votes
1 answer

Why does `ranges::view::for_each` require the functor must return a model of the `InputRange` concept?

#include #include #include using namespace ranges; int main() { auto coll = std::vector{ 1, 2, 3 }; std::for_each(coll.begin(), coll.end(), [](auto){}); // ok coll | view::for_each([](auto){});…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
10
votes
2 answers

ranges of ranges to vector of vectors

Suppose I have a range of T's called rng. I can do auto groups = ranges::view::group_by(rng, bin_op); groups now being a range of ranges of T's. I can also do this auto groups = ranges::view::group_by(rng, bin_op) | ranges::to_vector; to get a…
Richard Vock
  • 1,286
  • 10
  • 23
10
votes
2 answers

Why couldn't I get the size of a range in range-v3?

I want to get the number of people whose name starts with 'T': #include #include #include using namespace ranges; int main() { const auto names = std::vector {"Tony", "Peter"}; std::cout…
Cu2S
  • 667
  • 1
  • 5
  • 21
10
votes
1 answer

Declaring global const objects in a header file

In Eric Niebler's range-v3 library, he provides a lot of headers that each have their own global function object. They are all declared in the same way. He provides a class template static_const: template struct static_const { static…
Barry
  • 286,269
  • 29
  • 621
  • 977
9
votes
0 answers

Is there a way in Range-v3 to prepend or append an element to a range/view?

Range-v3 has ranges::views::drop and ranges::views::drop_last to remove elements from the front or the back of a view. Does it offer a similar functions to prepend/append elements to a view? For now, the shortest way I've found is to concat the…
Enlico
  • 23,259
  • 6
  • 48
  • 102
9
votes
2 answers

Can I return a temporary piped to a range operation?

Suppose I have a generate_my_range class that models a range (in particular, is regular). Then is the following code correct: auto generate_my_range(int some_param) { auto my_transform_op = [](const auto& x){ return do_sth(x); }; return…
Bérenger
  • 2,678
  • 2
  • 21
  • 42
9
votes
0 answers

What is the return type of a function returning a range?

This function can be compiled as is. auto works well here. But what would the explicit return type of such a function be? auto rangeTest() { static const std::vector vi{1,2,3,4,5,6,7,8,9,10}; auto rng = vi |…
dani
  • 3,677
  • 4
  • 26
  • 60
1
2
3
20 21