On 17.11.2011 21:48, Georg Baum wrote:
Peter Kümmel wrote:
Isn't there a better C/C++ way of solving this? For instance, couldn't it
somehow be 'transformed' into a switch/case statement?
A proper parser would of course use something like an enum for each string
literal (ideally automatically generated from a formal grammar, as well as
the big switch statement). TeX however is such a difficult language that I
fear that such a solution is not realistic. Therefore I think that the
current somewhat ad-hoc approach is not too bad.
What does boost say?
Found this:
http://arcticinteractive.com/2009/04/18/compile-time-string-hashing-boost-
mpl/
switch (boost::hash_value("bunnies"))
{
case meta::hash_cstring< mpl::string<'bunn','ies'> >::value:
std::cout<< "bunnies!\n";
break;
The idea is nice, but the 4-character restriction makes it quite unreadable
IMHO.
Currently the default size is 32 and it could be changed by a macro:
www.boost.org/doc/libs/1_48_0/libs//mpl/doc/refmanual/limit-string-size.html
I assume it will run faster because computing the hash and then jumping to the
case statement should be faster than to do a lot of string compares.
(boost 1.48 is out!)
Peter