I have an API that needs to be as simple as possible. At the same time, strings to certain functions are "well known", and so are always string literals, and can therefore be turned into CRC32 values at compile time, like so:
function(crc32("text"), ...);
The checksumming happens at compile-time using constexpr.
I want to simplify this API so that the people I work with don't have to know this pointless details. I want to make the compile-time checksumming happen inside the function, like so:
function("text", ...);
Doing the checksumming inside the inline function does not work because the function argument is no longer constexpr. The following code will fail to compile:
inline void function (const char* text, ...) {
constexpr uint32_t hash = crc32(text); // does not work
}
This is for an emulated environment so its crucial that the checksumming happens at compile-time. There are several functions that work like this, and they will all benefit from a simplification of the API.
What options do I have to hide the fact that you have to remember to use the call to crc32?
Using rustyx idea, I made this:
struct AutoCRC {
constexpr AutoCRC(const char* str) : hash{crc32(str)} {}
constexpr AutoCRC(const uint32_t h) : hash{h} {}
const uint32_t hash;
};
But it didn't work. Compared to using a constexpr CRC32 hash directly this almost doubled the binary size: 1488 -> 2696.
I have some limited C++20 access with GCC 9.2.