before the definition of my token, but when I use EnumClassHash, it complains that:
Error C2440 'static_cast': cannot convert from 'T' to 'size_t'
What is the proper way to implement enum as an unordered map key or value? (I assume that it should even work implicitly, since enum pretty much acts like an integer...)
Well I am not sure of the full scope of what you are doing, but for different problems there are differen't containers you need.
The container for enums are obvious, an array or a (filled) vector. Except for strings, you can use string keys with std::map, but I should note that unordered_map is slower than map for small lists, like around less than 20, because std::map isn't a hash map, it just loops through a list or something like that, and it is quite fast.
Also yes enums are plain ints.
function_to_type would work if you removed the 3rd template argument because you are using the 2nd argument as the hash, when the hash is the first argument, also you never need to use the 3rd argument unless you are creating a custom hasher, since std::hash is always used by default when you only have 2 arguments.
For type_to_function, what JLBorges said, I didn't read the error as a hash template error it would help if you shown more, maybe something that compiles, but I bet it is because double(*)(std::vector<token>&) doesn't match the functions "plus" or another, most likely because these functions are not static and are inside of a struct/class without a static on it.
Note: This custom hash function is not required in C++14 or later;
the standard library provides std::hash specialisations for scoped and unscoped enumeration types
poteto: you're completely right, since the enum entries are not used as keys in function_to_type, no additional hash is needed for this specific unordered map :) Since you asked, I'm using these unordered maps to (presumably) quickly look up a function in an infix evaluation, especially when I need to evaluate an expression many, many times. I think it's still too slow (definitely slow, compared to a compiled function), but what can I do :/
JLBorges: Your solution works, thank you! Would you please help me enable C++17 features in Visual Studio? My project properties under C/C++ - Language C++ Language Standard already reads ISO C++17, but the compiler seemingly struggles to find the default hash for enum (which should work like the one for integers, since enums are just integral values)
Have your solution/project open in the IDE. Bring up the project's property pages, open up the C/C++ submenu. On the Language property page select /std:c++17 or /std:c++latest for the C++ Language Standard.
Both 2017 and 2019are compliant with C++17's core language, there are a few defect reports not addressed.
I have both 2017 and 2019 installed, and when I fired up 2017 a few minutes ago to check how to set the language standard there was an "alert" to install 2019.
If someone wants C++20 support, then 2019 has more features implemented than 2015 or 2017.