85 points · by amazing42 · 4y ago · 44 comments
Try it at https://godbolt.org/z/8YMr8Kz8j
I hope I never see this in a production codebase, though!
On a kind of related note, I want to see an alternate history where C++ had support for Rust-ish proc macros, or Racket-ish macros. What would that language even look like? There'd probably be no need to hack existing C++ language semantics to add new features, you'd "just" generate the code that the meta-language lowers to.
Why? Looks completely readable, easy to understand to me.
Do you also hope to never see the STL in production? Because internally, that thing is high level unreadable C++.
I've written my fair share of code like this, and while you and I might grok it (and probably have a fun time figuring out how it works in the process), most people will hit a brick wall the moment they have to debug the 1000 line error that Clang or GCC will give you when there's a type error.
This is the kind of thing that belongs in language-feature land (so you get tooling support, reasonable compiler errors, etc), not library-land.
Again bringing up my STL example: This is just not how C++ runs. I’ve seen my fair share of std::__v1::basic_string<char, char_traits<char>, DefaultAllocator<>> errors. Some would argue a string type should be language-level, and they might be right, but the committee disagrees.
IMHO this "no junior will ever understand that" attitude comes mostly from older folks who learned C++ as C with classes and to whom even the STL is a work of the devil.
Perhaps unsurprisingly the author of this library looks to have only a few years of experience themselves.
SGI/HP STL isn't nearly as bad as modern implementations, though they all __share _Weird ____identifier names, mostly because of C/C++ identifier rules (_[A-Z] and anything with __ are reserved for the implementation, everything else might just be #define'd to rick-roll by whatever program is including you).
But MSVC latest instead generates the full runtime code and function call.
Should this be reported anywhere?
As a quick test, I added a static_assert() that depends on the returned value, which seems to force complete evaluation to happen at compile time. With that, all 3 compilers generate the expected single-instruction implementation: https://godbolt.org/z/onMannzM5
Although if the C++ committee manages to add this without deprecating other things, I would be impressed.
Also, unlike for regular expressions what you actually want here is typically a language feature, even if a bunch of the lifting is done in your standard library. In particular you probably want a keyword (or in several C++ pattern matching proposals, more than one) and new behaviour not just a few functions and constants.
You could think of this as a bit like "switch" but on the other hand if your insight into switch is that it's basically a computed go-to wearing fancy dress then no, not that, the thing ordinary people use switch for.
const fn factorial(n: u128) -> u128 { match n { 0 => 1, _ => n * factorial(n-1) } }
fn main() { dbg!(factorial(20)); }
Rust does not support view pattern (called in Haskell) or app pattern (called in Racket). And that has been implemented in this library.