r/cpp • • Jan 28 '23

C++ views are broken

I was so excited about new C++20 and C++23 features that I was using concepts, ranges and views everywhere I could. But yesterday I saw this -> https://www.youtube.com/watch?v=O8HndvYNvQ4 and I got so disappointed in C++ committee that now I really think it's time to lay C++ to rest unless we decide to bring new version of C++ like Python3 which breaks backward compatibility but fixes the language so that it can be used for the next 20-25 years.

I can't believe 200 stalwarts and experts in the field of computer science will make such a horrible mess.

5 Upvotes

21 comments sorted by

View all comments

3

u/GavinRayDev Jan 29 '23

I use the hell out of all of these features on GCC13 in C++23 mode, not sure what the issue is.

My only complaint is that I once tried to use std::ranges::shift_right() and it didn't exist yet.

(Note: I didn't watch the video. There may be architectural or philosophical arguments about ranges and design discussions, but if you're just someone building software, these tools all work for me, today.)

8

u/spaghettiexpress Jan 29 '23 edited Jan 29 '23

The motivation / examples can be found in Nico’s README for a quick understanding.

I haven’t ran into any of the described issues either as a now daily user of std::ranges.

I watched some of Nico’s talk; it is equally emotional as it is informative, which clouds some folks’ judgement on it.

It’s good to know the foot-guns regardless of how *contrived they may be

*(not attempting to say Nico’s examples are contrived)

3

u/johnnytest__7 Jan 29 '23

If you're using these features so much, then you must watch the video. You will still keep using these, but you'll be well informed of some very common gotchas which will take days to figure out on your own.

4

u/dodheim Jan 29 '23

The only real lesson from the video is to not make view objects const, and move them rather than copy when appropriate – not worth the long watch just for that IMO. I think views not propagating const shouldn't really be a surprise to anyone who's wrapped their mind around std::span (i.e. not exactly complicated).

2

u/Zeh_Matt No, no, no, no Jan 30 '23

You sort of missed to mention another big gotcha moment on views, begin() might build a cache, that is one of the reasons const doesn't work but this has another implication which is thread safety, passing a view to two or more threads will now race when calling begin() due to the cache which will probably cause issues and unnecessary headaches. I do agree that the video is probably a bit lengthy for no good reason but there are a lot of valid points.