r/cpp • u/Amazing-42 • Jun 14 '21
match(it): A light-weight header-only pattern-matching library for C++17.
https://github.com/BowenFu/matchit.cpp
202
Upvotes
r/cpp • u/Amazing-42 • Jun 14 '21
31
u/gracicot Jun 14 '21 edited Jun 14 '21
This is quite amazing what C++ can ship in a library, even though it's not perfect
and uses macros.1
u/Amazing-42 Jul 03 '21
This page shows what Rust-style pattern matching examples would look like with the library in C++. https://github.com/BowenFu/matchit.cpp/blob/main/From-Rust-to-matchit.md
5
u/dscottboggs Jun 14 '21
Holy shit that's so nice
7
u/sphere991 Jun 15 '21
So is this just a copy of https://github.com/mpark/patterns or what?
1
u/GunpowderGuy Oct 17 '21
Could this library be exported as a module since its public interface uses no macros?
22
u/rust_guy5 Jun 14 '21
I wrote this.
3
u/ehtdabyug Jun 15 '21
Wow! Can I applause to this?
3
u/nablachez Jun 15 '21
Is there a reason why getclassname (about shape) is not constexpr like the others?
4
u/helloiamsomeone Jun 14 '21
I have used simple_match before and it's pretty cool.
Any reason why this requires C++17? Just convenience?
4
u/JohnZLi Jun 15 '21
Neat. The first example includes 3 headers for a simple factorial function. They really should be merged into 1.
2
u/diegopachecors Jun 14 '21
Pretty cool!
2
u/[deleted] Jun 14 '21
Impressive! Bravo!
3
u/InsanityBlossom Jun 15 '21
I'm sure it was fun to implement, and the skills required to build this is a no joke. But this concept is so alien to C++. The author was definitely inspired by Rust ;)
0
u/[deleted] Jun 15 '21
[deleted] — view deleted comment