r/cpp • u/Deep_Wash_8310 • Dec 19 '22
Are there likely to be any changes to views to fix the issues raised by Nicolai Josuttis?
Just watched the (very good) talk from Nicolai Josuttis where he mentions several seemingly severe issues with views https://www.youtube.com/watch?v=O8HndvYNvQ4. I was curious what the reaction in the C++ community has been since the talk. Is there mostly agreement that this is a massive problem? If so, is it likely to be fixed? Or are we now stuck since any change will either break backwards compatibility or require adding a second official view library (yuck)?
I use C++ regularly at work but am definitely not a C++ expert and only just started investigating the new features from C++17 onwards so I can easily imagine that there is another side to the story. I also appreciate all the time that super talented people sacrifice to try and make the language better. Nevertheless I have to say that I struggle to understand how the standards committee thought examples like these were only minor or non-issues. The first hour for const propagation dampened my excitement by a fair amount. To my surprise this wasn't even the worst part, after I saw that it is (usually) undefined behaviour to modify elements when using a predicate filter I genuinely started to worry for the future of C++ (and yes I realise that there have doubtless been hundreds of people predicting the demise of C++ on a daily basis for the past 30 years :P)
113
u/BarryRevzin Dec 20 '22
Indeed. The three major things Nico is complaining about are:
And Nico argues that basically there is absolutely no reason for either of these things.
But (1) is kind of inherent in the model. There are some views for which iteration must mutate them. A view that reads elements from stdin, for instance (
views::istream) mutates itself as it goes, that cannot possibly be const-iterable. A view that generates new elements by repeatedly invoking a function that changes its own state cannot possibly be const-iterable. A coroutine generator. A view that caches elements for performance. A view that has to keep additional state as its going (like wanting tojoina range of prvalue ranges - you have to store each prvalue range somewhere). All these things exist and are pretty useful utilities. This last one in particular in something people run into a lot - that's flat map. That's like:In order to have a model where "all views shall be const-iterable," you can't provide any of that. You just can't. Mutation is inherent there. So that kind of sucks as a model, since the whole basis for C++ iterators is to be able to provide as much functionality as possible (even if that makes them complicated).
So that means we'll always have some views that aren't const-iterable. But at that point, if you want to operate on all ranges, you have to be able to operate on non-const-iterable views. And, I get it - that kinda sucks and is tedious. But the choice isn't really to avoid the issue. You either want the functionality or you don't, and I definitely want the functionality. Maybe Nico doesn't want the functionality, but you can't present the issue as if there isn't an inherent tension here - like there's no choice.
Mind you, no D range is const-iterable, and that's the closest model to C++20 ranges. Python and Rust iterators aren't const-iterable either.
For const propagation, sure. An alternate view design could propagate const. There's no technical issue with this that I'm aware of. Although, if anything, I think it might've been simpler to have gone the complete other extreme and make no view const-iterable - that way all the views are consistent and you have a lot less code to have to write for a given range adaptor. But if you want to make const-propagating views, you could do that just fine. You could even write a range adaptor that does const-propagation, it's pretty easy to write, probably less than 20 lines of code. But is that... useful? The reason that views don't propagate const is that views are generalizations of types that look like this:
And that assignment there works, because that's how pointers (don't) propagate const. And sure, you could argue that just because the language does this doesn't mean that the library should - and I think there's an interesting discussion to be had here. I just don't think Nico is pushing that discussion. He's just saying everything is broken.
Well, because a lot of the examples are not actually good examples. It's very easy to present any design as bad if you spend two hours talking about weird edge cases that nobody writes and call people's attention to things without making any attempt to explain... as far as I can tell, anything.
For instance, at some point he points out that in C++23,
std::ranges:cbegin(c)will actually give you a constant iterator whilestd::cbegin(c)may not necessarily do so. Why did we makestd::ranges::cbegindo the right thing but notstd::cbegin?Because I'm an asshole that hates users and wishes to watch them suffer, obviously. According to Nico anyway.
No, the real reason is that
std::cbeginis specified in a way that makes it very difficult to make this change in a way that won't break code. The thing is, we just say thatstd::cbegin(c)callsstd::as_const(c).begin()and returns whatever that is. We don't do any kind of checks on the type ofc, whether it has any members, whether the result is an iterator at all, nothing. I mean, hopefully people only call this on containers, but it's not like they have to. In order to be able to make this "do the right thing", we need to start checking things aboutc, and some of those checks may not be SFINAE-friendly (if the user who wrote the typeCdidn't care about making them SFINAE-friendly, because he didn't need to do this until now), and that might start breaking otherwise perfectly fine code.So I thought it was better to just improve the new facility to make it better, rather than also spending a lot of time trying to improve the old facility, which is worse anyway even with this change, so is it even worth improving the old facility in a way that might break old code? Naw, users can just use the new one. And honestly, if the real crux of the issue is people wanting to mutate through a call to
std::cbegin, just... don't?Really, this is frustrating because I spent a tremendous amount of time on this paper because these things aren't exactly easy, and to have all that work be reduced to like:
Yeah, sorry. I consider that a non-issue.
21
u/ben_craig freestanding|LEWG Vice Chair Dec 19 '22
This paper fixes the issue, and has been accepted into C++23 as of November. So yes, there are changes to fix the issue.
EDIT: The above paper fixes the dangling issues. Depending on who you ask, the const stuff is or isn't an issue. It's the difference between
char const *andchar * const.20
u/VinnieFalco wg21.org | corosio.org Dec 22 '22
Yes I am definitely going to do something about this. I will take all of Nicolai's advice and proceed to ignore it. While he is writing books and complaining about nothingburgers I will continue to write more libraries that rely heavily not only on string_view, but that also offer many new view types / reference-like types. I am reminded of the play by William Shakespeare. What was it called... oh yes: "Much Ado About Nothing."
6
u/cristi1990an ++ Dec 20 '22
Does anyone have any idea why the standard is so adamant on requiring begin/end to be amortized constant time? I understand why this was an easy guarantee to give for containers, but what's so important about this guarantee that they can't change it for views?
Yes, views shouldn't cache, I haven't heard any good argument in favor of this so far. The downsides far outweigh the benefits.
6
u/squirrel428 Apr 21 '23
I'm just a normal user that was really excited about ranges till I started trying to use them and ran into the const stuff that broke all my template functions.
When I heard they were lazily evaluated I thought the advantage was I could create a bunch of them and then only iterate through them when I needed their output, but then I discovered caching in some of them.
I think I now understand that they are only lazy sometimes and other times cache with the intention of only being used as temporaries.
One thing that really confused me at first was Berry's talk comparing C++ with Rust iterators. The iterator trait works with containers like vector where it might be optional to separate advancing iteration and dereferencing you can without doing the other. As a result Rust doesn't lose the expressiveness he claims it does and it even has a friendly way to express interior constness.
I read the arguments here by Berry and in other places about why views work the way they do and I'm starting to understand the other side of this. I strongly disagree though that any of this is user friendly. I have heard someone say the moto of C++ should be "you are holding it wrong". Maybe that's my problem, I am always holding it wrong and so it hurts me.
Another thing I find kind of magical about the ranges library is the functions that return the view types. I think there is a real lost opportunity to fix the algorithmic complexity problems by overloading these functions on various input types and transforming the types they output. This could have made the case for caching less compelling.
8
u/Voltra_Neo Dec 19 '22
The zip example got me so mad. How is that allowed in the standard?
std::function's call operator was one thing, but this just ruinsconstorviewsdepending on which you value less