c++ - constexpr in C (or equivalent) -
i trying string-based switch expression work in c using hash function. i've been able work clean syntax using 'constexpr' clang/llvm turned c++, though code c.
however, there of course odd side effects of having compile c++, lack of void* implicit casting becomes awkward.
so question how solve dilemma (without slapping c11 committee upside head why wasn't added c spec)
- is there way constexpr option turned on c?
- is there way implicit void* casting turned on c++?
- is there clean way code in c11/c99 doesn't require recalculating hashes?
here current example code:
constexpr uint64 chash(char const* text, uint64 last_value = basis) { return *str ? chash(text+1, (*text ^ last_value) * prime) : last_value; } void switchfunction(char const* text) { switch(hash(text)) { case chash("first"): break; case chash("second"): break; case chash("third"): break; default: break; } }
is there way constexpr option turned on c?
no, no such thing exists in c.
is there way implicit void* casting turned on c++?
no, c++ has mandatory type safety of pointers.
is there clean way code in c11/c99 doesn't require recalculating hashes?
the way can it, traditional way macros. in case create function-like macro parameters, , use on compile-time constants, computations done @ compile-time. unfortunately, code turn rather ugly, there no way avoid in c.
the best way might prepare such compile-time parameters external script/program, store them raw data tables in c program.
Comments
Post a Comment