I have a constexpr function that calculates CRC at compile time. I need to create a map between CRCs that are generated by this function and string inputs and use them in runtime (Even better if it is also possible to use it in compile time too).
The map creation process should be from withing this constexpr function so it automatically logs all CRCs that are generated. Also it doesn't necessarily needs to be a map. Only key value pairs that could be looked up.
I'm using C++ 17 by the way.
This is my function signature:
template <size_t length>
static constexpr unsigned int getCrc32(const char (&str)[length])
{
//calculate compile time CRC
// What I need should happen here: saving the calculated
// CRC and string to be accessed outside later
}
I tried different ways with templates and other things but I either end up with compiling code with undefined behavior or unmodifiable contexpr wall.
constexpr std::map, you can haveconstexpr std::array<std::pair<unsigned int, const char*>>(with pointer with static storage).constexpr Result r1{"abc", getCrc32("abc")};assuming you have somestruct Result {const char* in; unsigned int out;};constexprfunction to modify some storage while (possibly runtime dependent) calling this function.