-1

In a pre-c++20 question Access index in range-for loop (answer), it was mentioned that

2020 note: It would probably be more wise to use a lambda-based solution instead of macro trickery.

Could someone post the lambda-based solution for c++20? Are there any other solutions new to c++20?


Edit: I have a main loop which is polymorphic over the size of a vector. Since I have to read and modify this piece of code frequently, I think I'm justified wanting the nicest solution. What happens if I want to use views::reverse sometimes in the main loop and not others? (I don't actually need this, and solved my immediate problem after refactoring, but still I'm using additional initializations so frequently to access the current element in cold development areas of my code base, it's worth finding a solution)

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
Tom Huntington
  • 2,260
  • 10
  • 20
  • 1
    Just use a good old non-range-based for loop. – n. m. could be an AI Sep 01 '21 at 06:16
  • 1
    "_Are there any other solutions new to c++20?_" - Perhaps there is something in `std::ranges`. Otherwise you could create a custom iterator. That's nothing special for C++20 though. I would have used that rather than using macros even in C++11 (and possibly even before that). If you're interested in that, please reformulate the question. – Ted Lyngmo Sep 01 '21 at 08:40
  • 2
    What about the [answer](https://stackoverflow.com/a/60209974/4944425) just above the one you linked (or the [accepted](https://stackoverflow.com/a/12397568/4944425) one), isn't it enough? – Bob__ Sep 01 '21 at 09:27
  • @TedLyngmo perhaps you could use ranges::subrange with your custom iterator, then your custom iterator could be used with a range-based for loops which could be considered a new solution for c++20. It would then be compatible with range adaptors which is something I want – Tom Huntington Sep 01 '21 at 21:41
  • 1
    @TomHuntington Then the range-v3:answer that Bob__ linked to should work well for you. – Ted Lyngmo Sep 01 '21 at 21:47
  • I think one solution is to define a view interface https://en.cppreference.com/w/cpp/ranges/view_interface which I'll try to post an answer for – Tom Huntington Sep 01 '21 at 22:13
  • Your question appears to be an *XY Problem*. See: [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – David C. Rankin Sep 01 '21 at 23:52
  • I'm not asking for help about the problem that gave me the motivation to post. I was just trying to justify why finding a c++20 ranges solution might be worth it, since no one seems to think it's justified – Tom Huntington Sep 01 '21 at 23:58
  • 1
    @TomHuntington I think it's justified to try to find a solution using as much of the standard library as possible. I have _very_ limited knowledge about `std::ranges` (and `ranges-v3`) but I now tried making an enumerator for fun, mimicing how I have understood how views work on a high level. I may have gotten it completely wrong though. :) [My try](https://godbolt.org/z/WrPh97bjG) – Ted Lyngmo Sep 02 '21 at 08:16
  • @TedLyngmo I'd be happy to accept that as an answer, the subtilties of library writing are beyond the scope of the question I think – Tom Huntington Sep 02 '21 at 09:41
  • 1
    @TomHuntington Thanks - but I'm not sure how to formulate that as an answer to the question "_Could someone post the lambda-based solution for c++20? Are there any other solutions new to c++20?_". I think it'll look odd - especially since my range-like enumerator works pre c++20 too :-) I think your own answer to the question is better. – Ted Lyngmo Sep 02 '21 at 09:44

1 Answers1

3

After looking into defining a view interface, I decided to use range-v3.

There are several things missing from std::ranges https://stackoverflow.com/a/68172842/11998382, and until they are added in future standards, it is sensible to use range-v3 rather than repeatedly attempting non-trivial implementations yourself.

enter image description here

Enlico
  • 23,259
  • 6
  • 48
  • 102
Tom Huntington
  • 2,260
  • 10
  • 20