Re:C++17 creates a practical use of the backward array index...
by tubs on Monday April 03, 2023 @05:27PM
(#35431519)
Problem with contains is we are now going to see more code like
over the (vastly uglier) more efficient:
Of course languages like C# manage this in a more elegant way with out parameters declarable at the call site.
[ Parent
| on HN ]
Re:C++17 creates a practical use of the backward array index...
by JonChesterfield on Monday April 03, 2023 @05:43PM
(#35431717)
A pity the c++ compiler has no way to recognise calls to the c++ library in order to do rewrites like that automatically. It's CSE at the stdlib level, should definitely be possible to do that.
Re:C++17 creates a practical use of the backward array index...
by eklitzke on Monday April 03, 2023 @06:38PM
(#35432326)
Hard coding behavior like for the STL that seems pretty questionable, especially given that std::map and std::unordered_map have poor performance compared to other alternatives (e.g. absl::btree_map and absl::flat_hash_map, and likewise folly has better implementations).
Re:C++17 creates a practical use of the backward array index...
by im3w1l on Monday April 03, 2023 @09:15PM
(#35433710)
if let is such a nice way to express this
if let Some(x) = map.get(foo) {
bob(x)
}
One subtle but noteworthy thing is that the idiom only mentions map by name once, which is nice if you have a very long map name, and prevents copy paste bugs where you only update one of the two mentions.
Re:C++17 creates a practical use of the backward array index...
by paulddraper on Tuesday April 04, 2023 @12:26AM
(#35435068)
Scala:
for (x <- map.get(foo)) {
bob(x)
}
// or
map.get(foo).foreach(bob)
// or
map.get(foo) match {
case Some(x) => bob(x)
}
Re:C++17 creates a practical use of the backward array index...
by olliej on Tuesday April 04, 2023 @02:59PM
(#35444381)
This pattern exists in C++ as well. The specific issue here is that all these STL APIs are in terms of C++'s iterator model, and can't be replaced with a more modern "optional" style that allows this cleaner coding style :-/