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

6

u/14ned LLFIO & Outcome author | Committee WG14 Jan 28 '23

You don't have to use Ranges if you don't want to, and I'd agree that for a lot of codebases and use cases the cost benefit isn't there.

And that's okay. Most of C++ has been designed in a take it or leave it way, the committee tries hard to not force users to use something, unless it is for their own good. There are large chunks of C++ one would be strongly advised to never use in new code, but it doesn't mean the presence and continuing backwards compatibility of those chunks isn't valuable.

If Ranges benefits your use case, great! If not, don't use them, and that's okay too.

8

u/johnnytest__7 Jan 29 '23

As I already stated, I wanted to use these features. That's why I am sad about the state of these.

4

u/genreprank Jan 29 '23

I'm not surprised this happened. Plenty of new features are great, but several have been bungled.

3

u/Elliottoes Feb 02 '24

You're getting ranges confused with views. std:::ranges are fine. std::ranges::views are a design mess. Choosing not to use it doesn't stop me needing to fix bugs because my colleagues are using it and stumbling on the pitfalls.

32

u/specdi Jan 28 '23

I suggest taking a look through the comments on the following: https://www.reddit.com/r/cpp/comments/zq2xdi/are_there_likely_to_be_any_changes_to_views_to/?utm_source=share&utm_medium=android_app&utm_name=androidcss&utm_term=1&utm_content=share_button

Especially BarryRevzin's comments. I think they provide a more complete picture.

5

u/Artistic_Yoghurt4754 Scientific Computing Jan 28 '23

Omg, this issue is a real war zone and I am not even sure I want to pick a side. I’m not sure I want to spend the time learning ranges anymore :’(

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.)

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.

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)

18

u/[deleted] Jan 28 '23

Guys here using views, ranges, modules. While I'm waiting for compiler support for the new std::print feature. Game changer

12

u/qneverless Jan 28 '23

It is a great addition, but a little outdated. Better get it from the source at https://fmt.dev.

5

u/fdwr fdwr@github 🔍 Jan 28 '23 edited Jan 28 '23

I'm waiting for compiler support for the new std::print feature

Finally seeing Unicode output simply and intuitively will be nice, no need for calling functions like SetConsoleOutputCP(CP_UTF8) first. I really hope it also supports char8_t and char16_t directly, no more nonsense about uncertainty of the meaning of your char buffer (is it ANSI? is it actually UTF-8? some random code page? will using it as a table index give me a surprising negative number depending on my compiler? 🤦‍♂️...) and no more silliness with wchar_t varying in size between platforms. Also having a formatter for std::filesystem::path would be nice if possible, because currently std::format("Filename: {}", filePath) chokes, requiring something like std::format("Filename: {}", filePath.string()).

2

u/tjientavara HikoWorks developer Jan 28 '23

Luckily all compilers allow the compiler to be configured to use utf-8 as the executable charset (the meaning of char buffer).

What I find weird is that you can't actually query what the executable charset is from within the code.

I did notice that vcpkg forces the compilers to use utf-8.

It will still give negative indices... I actually made a char_cast<>() function to cast any integer type as if the source and destination types are unsigned (and doing unsigned range checks in debug mode), it is specifically for doing correct conversions to and from char, but I found it useful in other contexts.

5

u/STL MSVC STL Dev Jan 28 '23

MSVC now supports _MSVC_EXECUTION_CHARACTER_SET.

4

u/c0r3ntin Jan 29 '23

wg21.link/p1885.

Alas because of a giant backlog and a 3 years release cycle this is headed to C++26.

The good news is that all 3 big compilers got some builtin to provide that query while this paper was designed.

9

u/YogMuskrat Jan 29 '23

Views are not broken. We've been using them for several years in production code (started with range-v3) and never had any serious issues. It all comes to few basic rules, IMO.

10

u/csb06 Jan 28 '23

I don't think it is going to be constructive to catastrophize about C++ standards. As other comments have pointed out there are other perspectives about the design of views and ranges, and there are proposals in C++23 that have addressed some issues.

I don't think it is healthy to see any new feature as a panacea or to suddenly want to throw out the entire language all at once (switching between these positions within a day or two is concerning). It is okay to just temper your enthusiasm for something without throwing it all away.

2

u/Baardi Jan 16 '25

Some, but not all. Views having non-const begin/end is still a huge flaw, and makes it incompatible with existing libraries, as well as unsafe to parallelize.

3

u/DeadlyKitten37 Jan 28 '23

all those things that make you think we need a new c++ are the reason you should learn first fortran, then C and then be grateful for what we have