Humouristic abuse of the C++ exception system for polymorphic return.
Presentation given at the distributed SwedenCPP Stockholm + London C++ UG meetup.
try { func(); } catch (A& a) { ... } catch (B& b) { ... } catch (...) { ... } So exceptions can give us the same desired behaviour as std::visit() does for std::variant<>
tokens Tokens for a very simple calculator. template <char> struct C {}; struct number { double value; }; struct ident { std::string_view value; }; struct eof {}; struct remember {}; struct forget {}; struct other {}; class lex { public: lex(std::string s); std::string next_token(); // throws token std::string peek(); // throws token void drop(); private: ... }; std::string lex::scan_token() { ... switch (*iter) { case '(': ++iter;throw C<'('>{}; case ')': ++iter;throw C<')'>{}; case '/': ++iter;throw C<'/'>{}; case '*': ++iter;throw C<'*'>{}; case '=': ++iter;throw C<'='>{}; case '+': ++iter;throw C<'+'>{}; case '-': ++iter;throw C<'-'>{}; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': return scan_number(); } return "unknown"; }