Constantly inlining a function with a consteval call

  c++, constants, consteval, constexpr, visual-c++

I have a consteval crc32 function that works just fine on compile time. I want this function to be used in another constant inline function.

Here is a code example for a better understanding:

unsigned int consteval crc32(const std::string str, size_t idx = -2) {
    if (idx == -2) idx = str.size() - 1;
    if (idx == -1) return 0xFFFFFFFF;

    return (crc32(str, idx - 1) >> 8) ^ crc_table[(crc32(str, idx - 1) ^ str[idx]) & 0x000000FF];
}

unsigned int consteval full_crc32(const std::string str) {
    return ~crc32(str);
}

void _printHash(const unsigned int hash) {
    printf("%d", hash);
}

inline void printHash(const std::string str) {
    _printHash(full_crc32(str));
}

int main(int argc, char* argv[]) {
    printHash("abc");
}

I want this code to compile to just:

int main(int argc, char* argv[]) {
    _printHash(/* hash constant */);
}

Is there any way to acheive this behavior on MSVC?

Source: Windows Questions C++

LEAVE A COMMENT