nikitas-theo/pcl

Switch statement with strings

Closed this issue · 0 comments

Can use this which seems to be fast : https://stackoverflow.com/questions/650162/why-the-switch-statement-cannot-be-applied-on-strings

/* Expressions */
constexpr unsigned int hashf(const char *s, int off = 0) {                        
    return !s[off] ? 5381 : (hashf(s, off+1)*33) ^ s[off];                           
}    
constexpr inline unsigned int operator "" _(char const * p, size_t) { return hashf(p); }

and then use like :

switch(hashf(op)):
... case "<="_ :  // here _ is actually an operator that hashes the string, for beautiful expressions 

Generally using op[0] doesn't scale well, and probably can't use it for both "<=" and "<".