Home > other >  Is using ranges in c advisable at all?
Is using ranges in c advisable at all?

Time:07-02

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 advent of ranges in the stl; however, as of C 20, there are severe shortcomings: the support for this among different implementations of the standard library varies, and many things present in range-v3 did not make it into C 20, such as (to my great surprise), converting a view into a vector (which, for me, renders this all a bit useless if I cannot store the results of a computation in a vector).

On the other hand, using range-v3 also seems not ideal to me: it is poorly documented (and I don't agree that all things in there are self-explanatory), and, more severely, C 20-ideas of ranges differ from what range-v3 does, so I cannot just say, okay, let's stick with range-v3; that will become standard anyway at some time.

So, should I even use any of the two? Or is this all just not worth it, and by relying on std ranges or range-v3, making my code too difficult to maintain and port?

CodePudding user response:

Is using ranges in c advisable at all?

Yes.

and many things present in range-v3 did not make it into C 20, such as (to my great surprise), converting a view into a vector

Yes. But std::ranges::to has been adopted by C 23, which is more powerful and works well with C 23's range version constructor of stl containers.

So, should I even use any of the two?

You should use the standard library <ranges>.

It contains several PR enhancements such as owning_view, redesigned split_view, and ongoing LWG fixes. In addition, C 23 brings not only more adapters such as join_with_view and zip_view, etc., but also more powerful features such as pipe support for user-defined range adaptors (P2387), and formatting ranges (P2286), etc. The only thing you have to do is wait for the compiler to implement it. You can refer to cppreference for the newest compiler support.

  • Related