获取'" str"'的价值通过' const uint8_t {aka const unsigned char}'常量表达式中的值

时间:2017-02-10 23:43:40

标签: c++ c++14 constexpr

假设以下示例

#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似乎产生了:

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");
                                     ^

如果我在constexpr之前删除了uint64_t v,那就可以了。但是,当constexpr出现时,我有点困惑。

另外,为什么a的参数char被视为uint8_tunsigned char

0 个答案:

没有答案