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
3
votes
0 answers

Why do I need to evaluate range-v3 views to be able to use them with concat?

I’m trying to concatenate multiple views on a move-only type: using namespace ranges; auto v = views::ints(0, 4) | views::transform([](int i) { return std::make_unique(i); }); auto w = views::ints(4, 8) | …
Michael Jung
  • 368
  • 1
  • 8
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
3
votes
2 answers

What is a view_closure in range-v3?

In a case like this: auto pow = [](int i) {return i * i; }; auto closure = ranges::views::transform(pow); closure seems to be a view_closure. I do get that last line does not make much sense, since transform is not applied anywhere. Actually, I…
gonidelis
  • 885
  • 10
  • 32
3
votes
2 answers

Why the standard defines borrowed_subrange_t as common_range?

C++20 introduced the ranges::borrowed_range, which defines requirements of a range such that a function can take it by value and return iterators obtained from it without danger of dangling. In short (which refer to P2017R1): A range is a…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
3
votes
0 answers

Would range-v3 be suitable for applying a function to certain indices in a vector?

Original vector: std::vector a{1, 2, 3, 4, 5, 6}; vector of indices: std::vector b{1, 3, 4}; Multiply by 10 each element in a that is indexed by the elements in b. Iterative implementation: for (auto i = 0; i < b.size(); ++i) a[b[i]]…
cocheci
  • 375
  • 3
  • 7
3
votes
1 answer

ranges::subrange in MSVC on Compiler Explorer

The following code compiles and runs just fine with GCC 8.3 and with Clang 10.0.1, whereas it fails miserably on MicroSoft shiny compiler. #include #include int main(){ std::vector
Enlico
  • 23,259
  • 6
  • 48
  • 102
3
votes
1 answer

Combining n vectors into one vector of n-tuples

I'm thinking about a function with signature template std::vector> join_vectors(std::vector&&...) { //... }; but probably a more general one accepting any iterable instead of just std::vector would be good.…
Enlico
  • 23,259
  • 6
  • 48
  • 102
3
votes
0 answers

Why can't I use a vector-of-floats like a vector-of-strings with range-joining?

This function: auto foo(const std::vector& vec) { auto func = [](auto&r) { return ranges::views::concat(r, r); }; return vec | ranges::views::transform(func) | ranges::views::join(',') |…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
1 answer

What is the difference between range-v3 views::drop and views::drop_exactly?

Can someone explain the difference between range-v3's view adaptors drop and drop_exactly? One difference I've observed is that if the number of elements in the range that is piped to these views is less than the argument to the view adaptors, drop…
cigien
  • 57,834
  • 11
  • 73
  • 112
3
votes
1 answer

Why thread sanitizer complains about this std::ranges::views::filter code?

When running this code thread sanitizer complains about data race. Why? #include #include #include #include int main(){ std::vector v{11,22,33,44,55,66}; auto view = v |…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
3
votes
2 answers

Add whitespace to string at specific places

I have a string "00000000000000000000010011010010". I want to add a whitespace after every eighth char to make it represent a 32 bit integer. For example: "00000000 00000000 00000100 11010010". What would be the simplest way to do this?
Roh
  • 311
  • 1
  • 10
3
votes
1 answer

In range v3 library, why ranges::copy do not work with output of ranges::views::chunk?

This works as expected: #include #include int main() { auto chunklist = ranges::views::ints(1, 13) | ranges::views::chunk(4); for (auto rng : chunklist) { for (auto elt : rng) std::cout << elt << " "; …
Konstantin Vladimirov
  • 6,791
  • 1
  • 27
  • 36
3
votes
2 answers

Using range-v3 to read comma separated list of numbers

I'd like to use Ranges (I use range-v3 implementation) to read a input stream that is a comma separated list of numbers. That is trivial to do without ranges but... This is what I thought was the straight-forward way to solve it: auto input =…
bamse
  • 55
  • 5
3
votes
4 answers

Does there exist a ranges::views::group_by counterpart that takes into account all elements, as opposed to just contiguous ones?

In C++20's std::ranges, we can expect getting views::group_by1. This can be very handy, but I found a problem while playing with it. From Eric Niebler's manual we can read that it "In essence, views::group_by groups contiguous elements together with…
Fureeish
  • 12,533
  • 4
  • 32
  • 62