How to implement universal switch/case, which can work for general C++ types as well and syntactically similar?
问题 In C/C++, switch/case compares only an integral type with a compile time constants. It's not possible to use them to compare user/library defined types like std::string with runtime values. Why the switch statement cannot be applied on strings? Can we implement look-a-like switch/case which gives similar syntactic sugar and serves the purpose of avoiding plain if/else comparisons. struct X { std::string s; bool operator== (const X& other) const { return s == other.s; } bool operator== (const