4

Assuming the following example

#include <cstdint>

constexpr uint64_t XYZ(uint64_t c, size_t n, const uint8_t * d) noexcept {
    return (c & n) + (*d); // dummy. this is actually a crc64 function
}

template < size_t N > inline constexpr uint64_t ABC(const char(&a)[N]) noexcept {
    return XYZ(0, a[N-1] == '\0' ? (N-1) : N, reinterpret_cast< const uint8_t * >(a));
}

int main(void)
{
    constexpr uint64_t v = ABC("str");
    return (int)v; // dummy
}

MinGW GCC 6.3 seems to generate:

main.cpp: In function 'int main()':
main.cpp:13:31:   in constexpr expansion of 'ABC<4ull>("abc")'
main.cpp:8:15:   in constexpr expansion of 'XYZ(0ull, ((a[3ull] == 0) ? 3ull : 4ull), ((const uint8_t*)a))'
main.cpp:13:37: error: accessing value of '"abc"' through a 'const uint8_t {aka const unsigned char}' glvalue in a constant expression
     constexpr uint64_t v = ABC("abc");
                                     ^

If I remove the constexpr before uint64_t v it works. But I'm a bit confused as to why it doesn't when constexpr is present.

And also why is parameter a which is char seen as uint8_t aka. unsigned char.

SLC
  • 2,167
  • 2
  • 28
  • 46

0 Answers0