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
5
votes
2 answers

range-v3: Adapting custom classes which already implement iterator interfaces (begin/end)

I have a custom container implementing begin and end. How can I pipe this container to ranges-v3 views? std::vector is pipeable, so I tried piping my custom class in the same way, but pipe operator is not found for my container. I looked into…
ofo
  • 1,160
  • 10
  • 21
5
votes
1 answer

How to get column-view and row-view from std::vector using range-v3 library?

Having a 7x5 matrix flattened to a std::vector, I want to get a view on columns and rows using Eric Niebler's range-v3 library. So far, I managed (room for improvement) to get a view on a single row, a single column and to connected rows. See:…
Porsche9II
  • 629
  • 5
  • 17
5
votes
1 answer

How do you declare a ranges-v3 view return value?

Currently, I can compose ranges-v3 views like this: auto v = ranges::view::reverse | ranges::view::filter([](int l){return l>5;}); But if I wanted to return v from a function I'd need to know its type. What is the type of a ranges-v3 view?
dicroce
  • 45,396
  • 28
  • 101
  • 140
5
votes
1 answer

Why does passing `const char[N]` and `const char*` to view::c_str() yield different binaries, while string_view produces the same?

With std::string_view, range::for_each yields exact assembly with both const char[N] and const char * passing to std::string_view ctor In other words, this code auto str = "the quick brown fox is jumping on a lazy dog\nthe quick brown fox is jumping…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
5
votes
2 answers

Can algorithms be made compatible with expression templates?

Suppose I have some array-based code that is enabled to be used by expression templates. E.g., I have operator[] for these arrays overloaded and also have overloaded the arithmetic operator + etc. Now I would like to let the STL algorithm any_of…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
5
votes
1 answer

Sorting Range-v3-zipped containers - can I unzip?

Is it possible to unzip previously zipped vectors using the C++ Range-v3 library? I would expect it to behave similarly to Haskell's unzip function or Python's zip(*list). It would be convenient, for instance, when sorting a vector by values of…
Floop
  • 451
  • 4
  • 10
4
votes
1 answer

Why can't `ranges::begin` be called on a `const any_view`?

I have this code that fails (Explorer): #include #include int main() { std::vector a{1}; const ranges::any_view av(a); ranges::begin(av); } And was in the…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
4
votes
0 answers

ranges-v3 much slower than raw for loop

I have a very basic example, just joining several strings into a bigger string, with a separator. I have implemented it using ranges and using a raw for loop, and measured performance using google benchmark. The ranges-v3 version is much slower than…
jjcasmar
  • 1,387
  • 1
  • 18
  • 30
4
votes
0 answers

Slice of an lvalue of a non-borrowed view does not make a copy of the view - is this intended behavior?

I'm having trouble understanding some ideas and design decisions in the v3 ranges library. A lot of the view adaptors (e.g. ranges::views::transform) make a copy of a view (or create a ref view to a container) they receive as input (by using…
ljudek
  • 41
  • 3
4
votes
1 answer

Ranges filter_view::iterator element modification results in UB

The initial question was why with the following code, std::vector coll{1,4,7,10}; auto iseven = [](auto&& i){return i % 2 == 0; }; auto colleven = coll | std::views::filter(iseven); // first view materialization for(int& i : colleven) { …
gonidelis
  • 885
  • 10
  • 32
4
votes
1 answer

What's the point of `viewable_range` concept?

[range.refinements] The viewable_­range concept specifies the requirements of a range type that can be converted to a view safely. It's mandatory implementation roughly states that a range further satisfies viewable_range if either it's simply a…
SedriX
  • 500
  • 3
  • 10
4
votes
5 answers

Is using ranges in c++ advisable at all?

I find the traditional syntax of most c++ stl algorithms annoying; that using them is lengthy to write is only a small issue, but that they always need to operate on existing objects limits their composability considerably. I was happy to see the…
Bubaya
  • 615
  • 3
  • 13
4
votes
1 answer

Lifetime of the returned range-v3 object in C++

I want to make a function that works like np.arange(). With range-v3, the code is auto arange(double start, double end, double step){ assert(step != 0); const auto element_count = static_cast((end - start) / step) + 1; return…
gk2
  • 67
  • 4
4
votes
1 answer

Is it possible to mix the 'range-v3' include(s) with the c++ 'ranges' include?

I have access to GCC 10 and compile with -std=c++20, and need views such as generate cache1, concat etc that aren't slated until possibly C++23. I wrote a simple program below that outputs '1 2 2 3 3 3' using range-v3. I tried to mix the range-v3…
4
votes
1 answer

Why the return view of C++20 range adaptors is not a constant expression?

Consider the following code: #include int main() { constexpr int a[] = {1, 2, 3, 4}; constexpr auto r = a | std::views::take(3); static_assert(*r.begin() == 1); } msvc accept it, gcc rejects it with: :5:44: error:…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90