Category : pattern-matching

Recently there was a need to write a matching pattern for catching errors, which it actually copes with. But the only thing it lacks in comparison with the same switch is the opportunity to fallthrough. All my attempts to implement this feature have been unsuccessful. Initially, I thought about something like this: matcher(errcode) .match(SQLITE_MISUSE , ..

Read more

Suppose I have a set of types : constexpr std::tuple<int,double,string> my_types; a set of values to identify them: constexpr std::array<const char*,3> my_ids = {"int","double","string"}; // const char* instead of string to be constexpr-compatible and an overload set template<class T> bool my_fun(my_type complex_object) { /* some treatment depending on type T */ } I have a ..

Read more

I’ve come across the pattern matching proposal in C++, and I tried to compile Example 4.6 about expression trees. Here’s the extracted code: #include <memory> #include <variant> struct Expr; struct Neg { std::shared_ptr<Expr> expr; }; struct Add { std::shared_ptr<Expr> lhs, rhs; }; struct Mul { std::shared_ptr<Expr> lhs, rhs; }; struct Expr : std::variant<int, Neg, Add, ..

Read more