I'm experimenting with the idea of creating a jump table at compile time that is then used during runtime. It would work something like so:
struct blah { constexpr blah() {} int operator()() const { return 42; }};
int fun(std::string const& str)
{
return switch_ {
case_{ "hello"_s, [](){ return 5; }
case_{ "blah"_s, blah{} }
}(str);
}
In this case, _s
is a constexpr string.
The idea is that this will create a hash table at compile time using a constexpr hash function.
I believe I have solved every issue with exception to one, which if it can't be solves probably makes this idea impossible to implement. That is initialization.
The hash is implemented with an array. Collisions will get placed in empty cells within that array. So then I am required to find a way to store all data types as a single type. Basic type erasure techniques don't appear to work. Also can't use placement new or cast the value into bytes and copy them into an array. Also tried to use a UB invoking union trick that generally would work at runtime but isn't allowed in constexpr context.
The only option that seems to be available is to render a union of sorts that is actually more like a tuple. So it only stores one value but occupies memory of all types in the hash. This doesn't seem good to me.
So, can anyone think of a way to store an arbitrary type into a constexpr generated array?